Archive

Monthly Archives: July 2013

What are the exit-codes of the following commands?

test 
test ""
test -z 
test -z ""
test -n 
test -n "" 

The interesting one is number five.

A=""
if  [ -n $A ];  then echo NONZERO_1; fi 
if [[ -n $A ]]; then echo NONZERO_2; fi 

Usually we put double quotes around variables to prevent word splitting. Here, we must double-quote the variable for a different reason. Do you really need conditions in your script?

For every days scripts container types are indispensable. Bash does not let you down on this: arrays are build-in, others can be coded for sure. Bash-arrays are not often used, maybe because plain arrays are not that useful, or maybe because the syntax is particular. One the following lines starting with echo outputs the length of the array defined in the first line.

A=(a1 a2 a3 a4 a5)
echo ${$A[#]}
echo @{$A[$]}
echo ${#A[@]}

See http://www.thegeekstuff.com/2010/06/bash-array-tutorial/ . Bash also can deal with associative arrays. At least latterly: http://www.linuxjournal.com/content/bash-associative-arrays .