Home > database >  Is there a ZSH equivalent to "shopt -s nullglob"?
Is there a ZSH equivalent to "shopt -s nullglob"?

Time:04-04

I'm currently working on a script that deletes all the PNG files from my Desktop. I want to create an array of file paths then use the rm command on each one.

This is the relevant bit of code:

#!/usr/bin/env bash

shopt -s nullglob

files=("$HOME"/Desktop/*.png)
files_found="${#files[@]}"

shopt -u nullglob

It has been recommend that I use shopt in case of no matching files.

However I'm on MacOS and just discovered that shopt is not available for ZSH. When I run the script I get command not found: shopt.

I've found the ZSH has an equivalent called setopt however after reading through the documentation I can't quite figure out which option is the correct one to use in the case. I can't seem to find any examples either.

Can anyone point me in the right direction?

CodePudding user response:

The corresponding option in zsh is CSH_NULL_GLOB (documented in man zshoptions).b

 setopt CSH_NULL_GLOB

(As far as I can tell, the idea of a pattern disappearing rather than being treated literally comes from csh.)

CodePudding user response:

Just come to the realisation that the issue of shopt not being found was due to me auto-loading the file as a ZSH function.

The script worked perfectly when I ran it like so:

bash ./tidy-desktop

Previously I had been running it just with the command tidy-desktop

Instead I now have this in my zsh_aliases:

tidy-desktop="~/.zshfn/tidy-desktop"

Thanks to @Charles Duffy for helping me figure out what was going on there!

  • Related