With Bash and Dash, you can check for an empty directory using just the shell
(ignore dotfiles to keep things simple):
set *
if [ -e "$1" ]
then
echo 'not empty'
else
echo 'empty'
fi
However I recently learned that Zsh fails spectacularly in this case:
% set *
zsh: no matches found: *
% echo "$? $#"
1 0
So not only does the
set
command fail, but it doesn't even set $@
. I suppose
I could test if $#
is 0
, but it appears that Zsh even stops execution:
% { set *; echo 2; }
zsh: no matches found: *
Compare with Bash and Dash:
$ { set *; echo 2; }
2
Can this be done in a way that works in bash, dash and zsh?
Asked by user327359
Jan 7, 2019, 01:06 AM
Last activity: Sep 27, 2023, 12:58 PM
Last activity: Sep 27, 2023, 12:58 PM