-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path21-parameter-expansion.sh
70 lines (55 loc) · 1.58 KB
/
21-parameter-expansion.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
echo "
######################################
## Example 20.1: #
## how to get the length of a string #
######################################
"
animal=panda
echo "${#animal}"
echo "
###############################
## Example 20.2: #
## how to do search & replace #
###############################
"
filename=swan.svg
echo "convert $filename ${filename/svg/png}"
echo 'You can also replace all instances of a string with //'
greeting="hello hello hello!"
echo ${greeting//hello/bonjour}
echo "
##################################################
## Example 20.3: #
## use a default value for an undefined variable #
##################################################
"
echo "Hello, ${name:-UNKNOWN NAME}"
name=Julia
echo "Hello, ${name:-UNKNOWN NAME}"
echo "
##################################
## Example 20.4: #
## remove a suffix from a string #
##################################
"
filename="motorcycle.svg"
echo ${filename%.svg}
echo 'or remove a prefix:'
filename="motorcycle.svg"
echo ${filename#motor}
echo "
####################
## Example 20.5: #
## get a substring #
####################
"
x="oh, hello there!"
echo ${x:4} # 4 is the offset
echo ${x:4:5} # 4 is the offset, 5 is the length
echo "
##################################################
## Example 20.6: #
## exit with an error if a variable is undefined #
##################################################
"
echo ${asdf:?"oops, not defined! danger!"}