If I run a code like this:
./script *.txt
*.txt
will be expanded to all files with .txt
extension. But if there is no such file, script will be called with *.txt
string. I wonder if there is a way to force expansion to "" when there is no such file. So it means if there is no such file, the script will be called without any argument.
Any idea?
nullglob
shell option (set withshopt -s nullglob
) - see Why is nullglob not default? – steeldriver Nov 24 '22 at 21:43./script ''
/./script ""
is not the same as./script
. The latter will call script with no argument, while the former will call it with one empty argument. – Stéphane Chazelas Nov 24 '22 at 21:47./script *.txt(N)
in zsh (where the nullglob option also comes from). Do you have to usebash
? – Stéphane Chazelas Nov 24 '22 at 21:48[ -e "$1" ]
if the first argument exists in the filesystem, and if it does, continue processing as usual? – Kusalananda Nov 24 '22 at 22:21