Some examples:
Traditional test:Traditional test:
foo="some thing"
# check if value of foo is not empty
if [ -n "$foo" ] ; then...
if test -n "$foo" ; then...
test and [ are commands like any others, so the variable is split into words unless it's in quotes.
New-style test
[[ ... ]] is a (newer) special shell construct, which works a bit differently, the most obvious thing being that it doesn't word-split variables:
if [[ -n $foo ]] ; then...
Some documentation on [ and [[ here.
Arithmetic test:Arithmetic test:
foo=12 bar=3
if (( $foo + $bar == 15 )) ; then ...
"Normal" commands:
All of the above act like normal commands, and if can take any command:
# grep returns true if it finds something
if grep pattern file ; then ...
Multiple commands:
Or we can use multiple commands. Wrapping a set of commands in ( ... ) runs them in subshell, which hascreating a copytemporary copy of the shell's state of the shell. (Assume that test_something tests something only in it's workingworking directory., variables). If we need to run some program temporarily in another directory:
# this will move to $somedir only for the duration of the subshell
if ( cd $somedir ; test_somethingsome_test ) ; then ...
# while here, the rest of the script will see the new working
# directory, even after the test
if cd $somedir ; test_somethingsome_test ; then ...