Monthly Archives: July 2013

What are the exit-codes of the following commands?

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

The interesting one is number five.

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 . Bash also can deal with associative arrays. At least latterly: .