esbej esbej - 3 months ago 15
Bash Question

Shell scripting - if statement difference

This a question of an exercise:
What is the difference between the two "if" instructions?

#!/bin/bash
rm tmp
echo -n > tmp
for f in $*
do
if test ! -f $f
then
echo $f does not exist as a file
continue
fi
rm $f

if [ ! -f $f ]
then
echo $f has been deleted successfully
fi
ls $f >> tmp
done
x='cat tmp | grep -c ^.*$'
echo result: $x

Answer Source

The square brackets are a synonym for the test command, instead of if test ! -f $f we can use if [ ! -f $f ].
Note: test is a command which takes expression and test or evaluates.