|
1 | | -# Bash Variables |
2 | | - |
3 | | -As in any other programming language, you can use variables in Bash Scripting as well. However, there are no data types, and a variable in Bash can contain numbers as well as characters. |
4 | | - |
5 | | -To assign a value to a variable, all you need to do is use the `=` sign: |
6 | | - |
7 | | -```bash |
8 | | -name="DevDojo" |
9 | | -``` |
10 | | - |
11 | | ->{notice} as an important note, you can not have spaces before and after the `=` sign. |
12 | | -
|
13 | | -After that, to access the variable, you have to use the `$` and reference it as shown below: |
14 | | - |
15 | | -```bash |
16 | | -echo $name |
17 | | -``` |
18 | | - |
19 | | -Wrapping the variable name between curly brackets is not required, but is considered a good practice, and I would advise you to use them whenever you can: |
20 | | - |
21 | | -```bash |
22 | | -echo ${name} |
23 | | -``` |
24 | | - |
25 | | -The above code would output: `DevDojo` as this is the value of our `name` variable. |
26 | | - |
27 | | -Next, let's update our `devdojo.sh` script and include a variable in it. |
28 | | - |
29 | | -Again, you can open the file `devdojo.sh` with your favorite text editor, I'm using nano here to open the file: |
30 | | - |
31 | | -```bash |
32 | | -nano devdojo.sh |
33 | | -``` |
34 | | - |
35 | | -Adding our `name` variable here in the file, with a welcome message. Our file now looks like this: |
36 | | - |
37 | | -```bash |
38 | | -#!/bin/bash |
39 | | - |
40 | | -name="DevDojo" |
41 | | - |
42 | | -echo "Hi there $name" |
43 | | -``` |
44 | | - |
45 | | -Save it and run the file using the command below: |
46 | | - |
47 | | -```bash |
48 | | -./devdojo.sh |
49 | | -``` |
50 | | - |
51 | | -You would see the following output on your screen: |
52 | | - |
53 | | -```bash |
54 | | -Hi there DevDojo |
55 | | -``` |
56 | | - |
57 | | -Here is a rundown of the script written in the file: |
58 | | - |
59 | | -* `#!/bin/bash` - At first, we specified our shebang. |
60 | | -* `name=DevDojo` - Then, we defined a variable called `name` and assigned a value to it. |
61 | | -* `echo "Hi there $name"` - Finally, we output the content of the variable on the screen as a welcome message by using `echo` |
62 | | - |
63 | | -You can also add multiple variables in the file as shown below: |
64 | | - |
65 | | -```bash |
66 | | -#!/bin/bash |
67 | | - |
68 | | -name="DevDojo" |
69 | | -greeting="Hello" |
70 | | - |
71 | | -echo "$greeting $name" |
72 | | -``` |
73 | | - |
74 | | -Save the file and run it again: |
75 | | - |
76 | | -```bash |
77 | | -./devdojo.sh |
78 | | -``` |
79 | | - |
80 | | -You would see the following output on your screen: |
81 | | - |
82 | | -```bash |
83 | | -Hello DevDojo |
84 | | -``` |
85 | | -Note that you don't necessarily need to add semicolon `;` at the end of each line. It works both ways, a bit like other programming language such as JavaScript! |
86 | | - |
87 | | - |
88 | | -You can also add variables in the Command Line outside the Bash script and they can be read as parameters: |
89 | | - |
90 | | -```bash |
91 | | -./devdojo.sh Bobby buddy! |
92 | | -``` |
93 | | -This script takes in two parameters `Bobby`and `buddy!` separated by space. In the `devdojo.sh` file we have the following: |
94 | | - |
95 | | -```bash |
96 | | -#!/bin/bash |
97 | | - |
98 | | -echo "Hello there" $1 |
99 | | - |
100 | | -``` |
101 | | -`$1` is the first input (`Bobby`) in the Command Line. Similarly, there could be more inputs and they are all referenced to by the `$` sign and their respective order of input. This means that `buddy!` is referenced to using `$2`. Another useful method for reading variables is the `$@` which reads all inputs. |
102 | | - |
103 | | -So now let's change the `devdojo.sh` file to better understand: |
104 | | - |
105 | | -```bash |
106 | | -#!/bin/bash |
107 | | - |
108 | | -echo "Hello there" $1 |
109 | | - |
110 | | -# $1 : first parameter |
111 | | - |
112 | | -echo "Hello there" $2 |
113 | | - |
114 | | -# $2 : second parameter |
115 | | - |
116 | | -echo "Hello there" $@ |
117 | | - |
118 | | -# $@ : all |
119 | | -``` |
120 | | -The output for: |
121 | | - |
122 | | -```bash |
123 | | -./devdojo.sh Bobby buddy! |
124 | | -``` |
125 | | -Would be the following: |
126 | | - |
127 | | -```bash |
128 | | -Hello there Bobby |
129 | | -Hello there buddy! |
130 | | -Hello there Bobby buddy! |
131 | | -``` |
| 1 | +# Bash Variables |
| 2 | + |
| 3 | +As in any other programming language, you can use variables in Bash Scripting as well. However, there are no data types, and a variable in Bash can contain numbers as well as characters. |
| 4 | + |
| 5 | +To assign a value to a variable, all you need to do is use the `=` sign: |
| 6 | + |
| 7 | +```bash |
| 8 | +name="DevDojo" |
| 9 | +``` |
| 10 | + |
| 11 | +>{notice} as an important note, you can not have spaces before and after the `=` sign. |
| 12 | +
|
| 13 | +After that, to access the variable, you have to use the `$` and reference it as shown below: |
| 14 | + |
| 15 | +```bash |
| 16 | +echo "$name" |
| 17 | +``` |
| 18 | + |
| 19 | +The above code would output: `DevDojo` as this is the value of our `name` variable. |
| 20 | + |
| 21 | +## Quoting variables |
| 22 | + |
| 23 | +You should **always wrap variable references in double quotes** (`"$name"`). This is one of the most important habits to develop in Bash scripting. Without quotes, Bash will perform **word splitting** and **globbing** on the variable's value, which leads to bugs and even security vulnerabilities. |
| 24 | + |
| 25 | +Here is an example of what can go wrong without quotes: |
| 26 | + |
| 27 | +```bash |
| 28 | +#!/bin/bash |
| 29 | + |
| 30 | +filename="my file.txt" |
| 31 | + |
| 32 | +# Wrong - Bash splits this into two words: "my" and "file.txt" |
| 33 | +touch $filename # Creates two files: "my" and "file.txt" |
| 34 | + |
| 35 | +# Correct - the quotes preserve the value as a single argument |
| 36 | +touch "$filename" # Creates one file: "my file.txt" |
| 37 | +``` |
| 38 | + |
| 39 | +Without quotes, if a variable contains spaces, wildcards (`*`, `?`), or other special characters, Bash will interpret them rather than treating the value as a single string. This can cause scripts to break or behave unpredictably. |
| 40 | + |
| 41 | +>{notice} **Rule of thumb:** Always use double quotes around variables: `"$name"`, not `$name`. The only common exception is inside `[[ ]]` test brackets, where word splitting does not occur, but even there quoting is a good habit. |
| 42 | +
|
| 43 | +## When to use curly braces |
| 44 | + |
| 45 | +You can also wrap the variable name in curly braces: `${name}`. This is **required** when the variable name is followed by characters that could be interpreted as part of the name: |
| 46 | + |
| 47 | +```bash |
| 48 | +greeting="Hello" |
| 49 | + |
| 50 | +# Without braces, Bash looks for a variable called $greetings (not $greeting) |
| 51 | +echo "$greetings world" # Prints: " world" (empty - no such variable) |
| 52 | + |
| 53 | +# With braces, Bash knows the variable name is just "greeting" |
| 54 | +echo "${greeting}s world" # Prints: "Hellos world" |
| 55 | +``` |
| 56 | + |
| 57 | +Curly braces are also required for arrays (`${array[0]}`), string slicing (`${name:0:3}`), and default values (`${name:-default}`). For simple cases like `echo "$name"`, the braces are optional, so both `"$name"` and `"${name}"` work. |
| 58 | + |
| 59 | +Throughout this book, we use curly braces when they are needed for clarity or disambiguation, and omit them when the variable stands alone. |
| 60 | + |
| 61 | +## Using variables in a script |
| 62 | + |
| 63 | +Next, let's update our `devdojo.sh` script and include a variable in it. |
| 64 | + |
| 65 | +Again, you can open the file `devdojo.sh` with your favorite text editor, I'm using nano here to open the file: |
| 66 | + |
| 67 | +```bash |
| 68 | +nano devdojo.sh |
| 69 | +``` |
| 70 | + |
| 71 | +Adding our `name` variable here in the file, with a welcome message. Our file now looks like this: |
| 72 | + |
| 73 | +```bash |
| 74 | +#!/bin/bash |
| 75 | + |
| 76 | +name="DevDojo" |
| 77 | + |
| 78 | +echo "Hi there $name" |
| 79 | +``` |
| 80 | + |
| 81 | +Save it and run the file using the command below: |
| 82 | + |
| 83 | +```bash |
| 84 | +./devdojo.sh |
| 85 | +``` |
| 86 | + |
| 87 | +You would see the following output on your screen: |
| 88 | + |
| 89 | +```bash |
| 90 | +Hi there DevDojo |
| 91 | +``` |
| 92 | + |
| 93 | +Here is a rundown of the script written in the file: |
| 94 | + |
| 95 | +* `#!/bin/bash` - At first, we specified our shebang. |
| 96 | +* `name="DevDojo"` - Then, we defined a variable called `name` and assigned a value to it. |
| 97 | +* `echo "Hi there $name"` - Finally, we output the content of the variable on the screen as a welcome message by using `echo`. |
| 98 | + |
| 99 | +You can also add multiple variables in the file as shown below: |
| 100 | + |
| 101 | +```bash |
| 102 | +#!/bin/bash |
| 103 | + |
| 104 | +name="DevDojo" |
| 105 | +greeting="Hello" |
| 106 | + |
| 107 | +echo "$greeting $name" |
| 108 | +``` |
| 109 | + |
| 110 | +Save the file and run it again: |
| 111 | + |
| 112 | +```bash |
| 113 | +./devdojo.sh |
| 114 | +``` |
| 115 | + |
| 116 | +You would see the following output on your screen: |
| 117 | + |
| 118 | +```bash |
| 119 | +Hello DevDojo |
| 120 | +``` |
| 121 | +Note that you don't necessarily need to add semicolon `;` at the end of each line. It works both ways, a bit like other programming language such as JavaScript! |
| 122 | + |
| 123 | + |
| 124 | +You can also add variables in the Command Line outside the Bash script and they can be read as parameters: |
| 125 | + |
| 126 | +```bash |
| 127 | +./devdojo.sh Bobby buddy! |
| 128 | +``` |
| 129 | +This script takes in two parameters `Bobby`and `buddy!` separated by space. In the `devdojo.sh` file we have the following: |
| 130 | + |
| 131 | +```bash |
| 132 | +#!/bin/bash |
| 133 | + |
| 134 | +echo "Hello there $1" |
| 135 | +``` |
| 136 | + |
| 137 | +`$1` is the first input (`Bobby`) in the Command Line. Similarly, there could be more inputs and they are all referenced to by the `$` sign and their respective order of input. This means that `buddy!` is referenced to using `$2`. Another useful method for reading variables is the `$@` which reads all inputs. |
| 138 | + |
| 139 | +So now let's change the `devdojo.sh` file to better understand: |
| 140 | + |
| 141 | +```bash |
| 142 | +#!/bin/bash |
| 143 | + |
| 144 | +echo "Hello there $1" |
| 145 | + |
| 146 | +# $1 : first parameter |
| 147 | + |
| 148 | +echo "Hello there $2" |
| 149 | + |
| 150 | +# $2 : second parameter |
| 151 | + |
| 152 | +echo "Hello there $@" |
| 153 | + |
| 154 | +# $@ : all |
| 155 | +``` |
| 156 | +The output for: |
| 157 | + |
| 158 | +```bash |
| 159 | +./devdojo.sh Bobby buddy! |
| 160 | +``` |
| 161 | +Would be the following: |
| 162 | + |
| 163 | +```bash |
| 164 | +Hello there Bobby |
| 165 | +Hello there buddy! |
| 166 | +Hello there Bobby buddy! |
| 167 | +``` |
0 commit comments