-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
07-variables.sh
55 lines (43 loc) · 1.36 KB
/
07-variables.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
#!/bin/bash
echo "
##########################
## Example 7.1: #
## how to set a variable #
##########################
"
x='hello there'
echo $x
echo "here's what happens when you put spaces around the =:\n"
x = 'hello there' # this causes an error
echo "
########################################################
## Example 7.2: #
## you don't always need to put quotes around strings: #
########################################################
"
x=banana
echo "but you do need quotes if there's a space"
x=hello there
echo "
##########################################
## Example 7.3: #
## why it's important to quote variables #
##########################################
"
echo "here's what happens if you don't put quotes around a filename with spaces:"
filename="files/filename with spaces"
cat $filename
echo "it works with quotes:"
cat "$filename"
echo "
###############################################################
## Example 7.4: #
## how to use ${var} to concatenate a variable with a string: #
###############################################################
"
x=panda
echo "${x}bear"
echo "${x}2"
echo "these doesn't work: there's no variable called 'xbear' or 'x2'"
echo "$xbear"
echo "$x2"