Debian Codegraph @

ex9.sh

raw symbols SCIP
1 #!/bin/bash
2 # ex9.sh
3
4 # Variables: assignment and substitution
5
6 a=375
7 hello=$a
8 # ^ ^
9
10 #-------------------------------------------------------------------------
11 # No space permitted on either side of = sign when initializing variables.
12 # What happens if there is a space?
13
14 # "VARIABLE =value"
15 # ^
16 #% Script tries to run "VARIABLE" command with one argument, "=value".
17
18 # "VARIABLE= value"
19 # ^
20 #% Script tries to run "value" command with
21 #+ the environmental variable "VARIABLE" set to "".
22 #-------------------------------------------------------------------------
23
24
25 echo hello # hello
26 # Not a variable reference, just the string "hello" ...
27
28 echo $hello # 375
29 # ^ This *is* a variable reference.
30 echo ${hello} # 375
31 # Likewise a variable reference, as above.
32
33 # Quoting . . .
34 echo "$hello" # 375
35 echo "${hello}" # 375
36
37 echo
38
39 hello="A B C D"
40 echo $hello # A B C D
41 echo "$hello" # A B C D
42 # As we see, echo $hello and echo "$hello" give different results.
43 # =======================================
44 # Quoting a variable preserves whitespace.
45 # =======================================
46
47 echo
48
49 echo '$hello' # $hello
50 # ^ ^
51 # Variable referencing disabled (escaped) by single quotes,
52 #+ which causes the "$" to be interpreted literally.
53
54 # Notice the effect of different types of quoting.
55
56
57 hello= # Setting it to a null value.
58 echo "\$hello (null value) = $hello" # $hello (null value) =
59 # Note that setting a variable to a null value is not the same as
60 #+ unsetting it, although the end result is the same (see below).
61
62 # --------------------------------------------------------------
63
64 # It is permissible to set multiple variables on the same line,
65 #+ if separated by white space.
66 # Caution, this may reduce legibility, and may not be portable.
67
68 var1=21 var2=22 var3=$V3
69 echo
70 echo "var1=$var1 var2=$var2 var3=$var3"
71
72 # May cause problems with legacy versions of "sh" . . .
73
74 # --------------------------------------------------------------
75
76 echo; echo
77
78 numbers="one two three"
79 # ^ ^
80 other_numbers="1 2 3"
81 # ^ ^
82 # If there is whitespace embedded within a variable,
83 #+ then quotes are necessary.
84 # other_numbers=1 2 3 # Gives an error message.
85 echo "numbers = $numbers"
86 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
87 # Escaping the whitespace also works.
88 mixed_bag=2\ ---\ Whatever
89 # ^ ^ Space after escape (\).
90
91 echo "$mixed_bag" # 2 --- Whatever
92
93 echo; echo
94
95 echo "uninitialized_variable = $uninitialized_variable"
96 # Uninitialized variable has null value (no value at all!).
97 uninitialized_variable= # Declaring, but not initializing it --
98 #+ same as setting it to a null value, as above.
99 echo "uninitialized_variable = $uninitialized_variable"
100 # It still has a null value.
101
102 uninitialized_variable=23 # Set it.
103 unset uninitialized_variable # Unset it.
104 echo "uninitialized_variable = $uninitialized_variable"
105 # uninitialized_variable =
106 # It still has a null value.
107 echo
108
109 exit 0