vendredi 31 juillet 2020

How can a string be both null and not null according to -z and -n tests? [closed]

I am writing a script where I need to check for existence of files with a certain pattern, and I encountered this peculiar, at least to me, behavior. If I test the output of ls command for non-existing files, it turns out to be true whether I use -z or -n test.

# if [ -z $(ls /path/to/file/*nosuchfiles* 2>/dev/null) ]; then echo yes;fi
yes
# if [ -n $(ls /path/to/file/*nosuchfiles* 2>/dev/null) ]; then echo yes;fi
yes

How is that possible? What am I missing here?

Aucun commentaire:

Enregistrer un commentaire