BASH quick reference
Like perl and javascript, when writing a bash script it is good to write some short directives at the top of your scripts:
set -o errexit
set -o nounset
And others for failing pipelines:
set -o pipefail
Debugging can also be made easier by using xtrace:
set -o xtrace
Arrays
Append an element
ary+=("$element")
Length of array (element count)
ary_len=${#ary[@]}
Remove an element, the first element and then the last element
unset 'ary[0]'
unset "ary[$((${#ary[@]}-1))]"
Get Pipe Results
It is often useful to know if certain parts of a pipeline failed, bash offers a number of different ways:
cat file |grep orange |tee xlcr
echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
But that relies on bash, a different way is using command grouping:
((((someprog; echo $? >&3) | filter >&4) 3>&1) | (read xs; exit $xs)) 4>&1
echo $?
Interesting no?