BASH -- and ksh, and sh -- care about spaces in a few other places as well.
This is wrong:
if["this"="this"]
then
echo "strings are identical"
fi
This is okay:
if [ "this" = "this" ]
then
echo "strings are identical"
fi
This is also okay:
if [[ "this" = "this" ]]
then
echo "strings are identical"
fi
[[ ]] are extended versions of [ ]. They support the same things [ ] do and more besides. Look for test operators in the advanced bash scripting guide to see what flags you can do, like if [ -d directory ] # Is 'directory' a directory?
if [ -z "str" ] # Is "str" a blank string? (no, that's "" )
Also, you can do things like this to process files efficiently without backticks:
while read LINE
do
echo "$LINE"
done < /path/to/file
you can substitute [ ] style tests for the 'read' statement there. In fact you can put [ ] statements anywhere you'd put a command, and vice versa. [ actually used to be a command, as in, there was actually a /bin/[ file which ran when you did if [ -d directory ]
Your system probably still has /bin/[ or something like that somewhere for compatibility, even though it's probably not using it anymore.
This means you can use if to detect whether a command succeeded or not without counting lines or checking its backticks output or anything like that. You can just do
if programname
then
echo "programname succeeded"
fi
! also works where you might expect it:
if ! programname
then
echo "programname failed"
fi
---------- Post updated at 01:26 PM ---------- Previous update was at 01:17 PM ----------
Another unique feature the bourne shell has and csh simply cannot do is this:
for X in 1 2 3 4 5
do
echo "$X"
done > outputfile
You can redirect or pipe the output of entire code blocks, not just individual shell statements.