Skip to content

Commit e7d5183

Browse files
authored
Minor book improvements and shellcheck CI (#194)
1 parent 0df2eb8 commit e7d5183

15 files changed

+1741
-1551
lines changed

.github/workflows/shellcheck.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ShellCheck eBook
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- "ebook/en/content/**"
8+
- "scripts/shellcheck-ebook.sh"
9+
pull_request:
10+
paths:
11+
- "ebook/en/content/**"
12+
- "scripts/shellcheck-ebook.sh"
13+
14+
jobs:
15+
shellcheck:
16+
name: Lint bash code blocks
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Install ShellCheck
23+
run: sudo apt-get update && sudo apt-get install -y shellcheck
24+
25+
- name: Run ShellCheck on ebook code blocks
26+
run: ./scripts/shellcheck-ebook.sh ebook/en/content
Lines changed: 167 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,167 @@
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+
```

ebook/en/content/005-bash-user-input.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Bash User Input
22

3-
With the previous script, we defined a variable, and we output the value of the variable on the screen with the `echo $name`.
3+
With the previous script, we defined a variable, and we output the value of the variable on the screen with `echo "$name"`.
44

55
Now let's go ahead and ask the user for input instead. To do that again, open the file with your favorite text editor and update the script as follows:
66

@@ -51,4 +51,4 @@ echo "Hi there $name"
5151
echo "Welcome to DevDojo!"
5252
```
5353

54-
Make sure to test this out yourself as well!
54+
Make sure to test this out yourself as well!

ebook/en/content/007-bash-arguments.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can pass arguments to your shell script when you execute it. To pass an argu
66
./devdojo.com your_argument
77
```
88

9-
In the script, we can then use `$1` in order to reference the first argument that we specified.
9+
In the script, we can then use `$1` in order to reference the first argument that we specified.
1010

1111
If we pass a second argument, it would be available as `$2` and so on.
1212

@@ -71,11 +71,7 @@ For example, let's create a script that prints out the name of the file and dele
7171

7272
echo "The name of the file is: $0 and it is going to be self-deleted."
7373

74-
rm -f $0
74+
rm -f "$0"
7575
```
7676

7777
You need to be careful with the self deletion and ensure that you have your script backed up before you self-delete it.
78-
79-
80-
81-

0 commit comments

Comments
 (0)