test2.sh below is almost same as test1.sh script except that 4 spaces denoted using <SPACE> below are missing and hence it doesn't work.
If you miss any one of these 4 space characters , the script won't work. Why ? I didn't expect this from a modern shell like BASH.
### the below are spaces in test1
count=5
while [<SPACE>$count -le 15<SPACE>]
do
echo $count
count=`expr $count<SPACE>+<SPACE>1`
done
# cat test2.sh
count=5
while [$count -le 15] # 2 spaces missing in this line
do
echo $count
count=`expr $count+1` # another 2 spaces missing in this line
done
#
# ./test2.sh
./test2.sh: line 2: [5: command not found
Because "[" is considered a command the same way ls is a command, not a special shell syntax. "[5" is a valid filename too -- if you had a file named "[5", it would run it, so if you don't mean "[5", don't tell it "[5" -- remember how shell syntax works, "command space arguments".
If you left spaces out of other commands, like lsfilename you certainly wouldn't expect that to work.
[ is not any regular parenthesis like in other programming languages. It's equivalent of the test command. And all the keywords that follow [ are arguments passed to the program [ ; including the ] which [ interprets as the "end of arguments".
It seems parantheses ( ) doesn't have the issue with spaces as I managed to use $((count+1)) without any issues.
So, [ character being misinterpreted as a command is not applicable for ( )
It's known as a pre-increment operator. It means, first increment the value in count and then use it.
On the contrary, there's a post-increment operator ((count++)); which means use it first and then increment it.
For beginners, just to be VERY aware '[' and 'test' are NOT quite the same although they do a similar thing...
They cannot __cross-pollnate__.
Last login: Tue Jan 20 18:45:37 on ttys000
AMIGA:barrywalker~> var="Bazza..."
AMIGA:barrywalker~> if test "$var" == "Bazza..."; then echo "$var"; fi
Bazza...
AMIGA:barrywalker~> if [ "$var" == "Bazza..."; then echo "$var"; fi
-bash: [: missing `]'
AMIGA:barrywalker~> # Oh dear '[' and 'test' are not quite the same...
AMIGA:barrywalker~> if [ "$var" == "Bazza..." ]; then echo "$var"; fi
Bazza...
AMIGA:barrywalker~> if test "$var" == "Bazza..." ]; then echo "$var"; fi
-bash: test: too many arguments
AMIGA:barrywalker~> # Oh dear 'test' doesn't allow a closing square bracket...
AMIGA:barrywalker~> _