Skip to content
Newer
Older
100644 60 lines (33 sloc) 1.04 KB
fcc5114 @petdance first draft of rebuilt scraps
authored May 30, 2012
1 # Potter's talk about bash 3/11/2008
2
3 Turn forgiveness to kick and screaming
4
5 declare -i variable
6
7
8
9 Calling like this
10
11 ! grep
12
13 says it's OK to fail, and override errexit
14
15 if [[ $Age > 20 ]] # BAD, a string comparator
16 # Age of 3 buys beer
17
18 if [ $Age > 20 ] # BAD, creates a file called "20"
19
20 if (( $Age > 20 )) # Correct
21
22
23
24 if [[ $a == z* ]] # True if $a start with "z"
25
26 if [ $a == z* ] # z* does shell globbing
27
28
29 use [ when you "want" to use file globbing
30 use (( for math
31 use [[ for everything else
32
33 Regexes didn't come along until 3.0, are only in [[ ]], using =~
34
35 if [[ $My =~ "the (....) (.....)" ]]
36 echo ${BASH_REMATCH[0]}
37 echo ${BASH_REMATCH[1]}
38 echo ${BASH_REMATCH[2]}
39
40
41 gotcha:
42 cp $srcfile $dstfile
43 cp -- "$srcfile" "$dstfile"
44
45
46 substrings
47
48 ${Var:0:3}
49 ${Var: -5:2}
50
51 ${Var#pattern} strips off the pattern
52 We are #1 because we give 100%
53
54 declare MyStr="/home/andy/my.sample.sh"
55 echo ${MyStr##*/}" # my.sample.sh
56 echo ${MyStr%.*}" # /home/andy/my.sample
57
58 ${Var/pattern/replace}
59 ${Var//pattern/replace} # multiple replace
Something went wrong with that request. Please try again.