Jump to Navigation

209 - Bash [: too many arguments

If you run the following script, you got an error sometimes.

if [ -e /tmp/* ]; then
   echo "files exist"
else
   echo "No file exists"
fi

This is using -e (existing a file check) that is working fine on individual files. This code works fine if the result is one file;
but if you have more files returned,  it fails with the following error:

line x: [: too many arguments

-e will not work with multiple results. workaround for this issue is :

files=$(ls /tmp/*)
if [ $files ]; then
  echo "Files exist"
else
  echo "No file exists"
fi

 

 

Linux:


Main menu 2

Story | by Dr. Radut