Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (33 sloc) 1.04 KB
# Potter's talk about bash 3/11/2008
Turn forgiveness to kick and screaming
declare -i variable
Calling like this
! grep
says it's OK to fail, and override errexit
if [[ $Age > 20 ]] # BAD, a string comparator
# Age of 3 buys beer
if [ $Age > 20 ] # BAD, creates a file called "20"
if (( $Age > 20 )) # Correct
if [[ $a == z* ]] # True if $a start with "z"
if [ $a == z* ] # z* does shell globbing
use [ when you "want" to use file globbing
use (( for math
use [[ for everything else
Regexes didn't come along until 3.0, are only in [[ ]], using =~
if [[ $My =~ "the (....) (.....)" ]]
echo ${BASH_REMATCH[0]}
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
gotcha:
cp $srcfile $dstfile
cp -- "$srcfile" "$dstfile"
substrings
${Var:0:3}
${Var: -5:2}
${Var#pattern} strips off the pattern
We are #1 because we give 100%
declare MyStr="/home/andy/my.sample.sh"
echo ${MyStr##*/}" # my.sample.sh
echo ${MyStr%.*}" # /home/andy/my.sample
${Var/pattern/replace}
${Var//pattern/replace} # multiple replace