I have the following lines of bash script to test whether a file exists:
MYAPPPATH=$(find $APPDIR -iname myapp* -print)
if [ -e $MYAPPPATH ]
then
echo "File exists"
fi
However when file starting with "myapp" does not exist, therefore MYAPPPATH='', the above check succeeds again.
See what happens when using set -x
and the file does not exist:
++ find /path/to/app -iname 'myapp*' -print
+ MYAPPPATH=
+ '[' -e ']'
+ echo 'File exists'
What is the reason for this?
What do I need to do in order to make this work as expected?
[[...]]
was copied bybash
fromksh
. It's also supported byzsh
. So it's not bash-specific (though it's not standard/POSIX sh syntax so should not be used insh
scripts). – Stéphane Chazelas Nov 20 '14 at 11:34