Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: ShellCheck eBook

on:
push:
branches: [main]
paths:
- "ebook/en/content/**"
- "scripts/shellcheck-ebook.sh"
pull_request:
paths:
- "ebook/en/content/**"
- "scripts/shellcheck-ebook.sh"

jobs:
shellcheck:
name: Lint bash code blocks
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install ShellCheck
run: sudo apt-get update && sudo apt-get install -y shellcheck

- name: Run ShellCheck on ebook code blocks
run: ./scripts/shellcheck-ebook.sh ebook/en/content
298 changes: 167 additions & 131 deletions ebook/en/content/004-bash-variables.md
Original file line number Diff line number Diff line change
@@ -1,131 +1,167 @@
# Bash Variables

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.

To assign a value to a variable, all you need to do is use the `=` sign:

```bash
name="DevDojo"
```

>{notice} as an important note, you can not have spaces before and after the `=` sign.

After that, to access the variable, you have to use the `$` and reference it as shown below:

```bash
echo $name
```

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:

```bash
echo ${name}
```

The above code would output: `DevDojo` as this is the value of our `name` variable.

Next, let's update our `devdojo.sh` script and include a variable in it.

Again, you can open the file `devdojo.sh` with your favorite text editor, I'm using nano here to open the file:

```bash
nano devdojo.sh
```

Adding our `name` variable here in the file, with a welcome message. Our file now looks like this:

```bash
#!/bin/bash

name="DevDojo"

echo "Hi there $name"
```

Save it and run the file using the command below:

```bash
./devdojo.sh
```

You would see the following output on your screen:

```bash
Hi there DevDojo
```

Here is a rundown of the script written in the file:

* `#!/bin/bash` - At first, we specified our shebang.
* `name=DevDojo` - Then, we defined a variable called `name` and assigned a value to it.
* `echo "Hi there $name"` - Finally, we output the content of the variable on the screen as a welcome message by using `echo`

You can also add multiple variables in the file as shown below:

```bash
#!/bin/bash

name="DevDojo"
greeting="Hello"

echo "$greeting $name"
```

Save the file and run it again:

```bash
./devdojo.sh
```

You would see the following output on your screen:

```bash
Hello DevDojo
```
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!


You can also add variables in the Command Line outside the Bash script and they can be read as parameters:

```bash
./devdojo.sh Bobby buddy!
```
This script takes in two parameters `Bobby`and `buddy!` separated by space. In the `devdojo.sh` file we have the following:

```bash
#!/bin/bash

echo "Hello there" $1

```
`$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.

So now let's change the `devdojo.sh` file to better understand:

```bash
#!/bin/bash

echo "Hello there" $1

# $1 : first parameter

echo "Hello there" $2

# $2 : second parameter

echo "Hello there" $@

# $@ : all
```
The output for:

```bash
./devdojo.sh Bobby buddy!
```
Would be the following:

```bash
Hello there Bobby
Hello there buddy!
Hello there Bobby buddy!
```
# Bash Variables

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.

To assign a value to a variable, all you need to do is use the `=` sign:

```bash
name="DevDojo"
```

>{notice} as an important note, you can not have spaces before and after the `=` sign.

After that, to access the variable, you have to use the `$` and reference it as shown below:

```bash
echo "$name"
```

The above code would output: `DevDojo` as this is the value of our `name` variable.

## Quoting variables

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.

Here is an example of what can go wrong without quotes:

```bash
#!/bin/bash

filename="my file.txt"

# Wrong - Bash splits this into two words: "my" and "file.txt"
touch $filename # Creates two files: "my" and "file.txt"

# Correct - the quotes preserve the value as a single argument
touch "$filename" # Creates one file: "my file.txt"
```

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.

>{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.

## When to use curly braces

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:

```bash
greeting="Hello"

# Without braces, Bash looks for a variable called $greetings (not $greeting)
echo "$greetings world" # Prints: " world" (empty - no such variable)

# With braces, Bash knows the variable name is just "greeting"
echo "${greeting}s world" # Prints: "Hellos world"
```

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.

Throughout this book, we use curly braces when they are needed for clarity or disambiguation, and omit them when the variable stands alone.

## Using variables in a script

Next, let's update our `devdojo.sh` script and include a variable in it.

Again, you can open the file `devdojo.sh` with your favorite text editor, I'm using nano here to open the file:

```bash
nano devdojo.sh
```

Adding our `name` variable here in the file, with a welcome message. Our file now looks like this:

```bash
#!/bin/bash

name="DevDojo"

echo "Hi there $name"
```

Save it and run the file using the command below:

```bash
./devdojo.sh
```

You would see the following output on your screen:

```bash
Hi there DevDojo
```

Here is a rundown of the script written in the file:

* `#!/bin/bash` - At first, we specified our shebang.
* `name="DevDojo"` - Then, we defined a variable called `name` and assigned a value to it.
* `echo "Hi there $name"` - Finally, we output the content of the variable on the screen as a welcome message by using `echo`.

You can also add multiple variables in the file as shown below:

```bash
#!/bin/bash

name="DevDojo"
greeting="Hello"

echo "$greeting $name"
```

Save the file and run it again:

```bash
./devdojo.sh
```

You would see the following output on your screen:

```bash
Hello DevDojo
```
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!


You can also add variables in the Command Line outside the Bash script and they can be read as parameters:

```bash
./devdojo.sh Bobby buddy!
```
This script takes in two parameters `Bobby`and `buddy!` separated by space. In the `devdojo.sh` file we have the following:

```bash
#!/bin/bash

echo "Hello there $1"
```

`$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.

So now let's change the `devdojo.sh` file to better understand:

```bash
#!/bin/bash

echo "Hello there $1"

# $1 : first parameter

echo "Hello there $2"

# $2 : second parameter

echo "Hello there $@"

# $@ : all
```
The output for:

```bash
./devdojo.sh Bobby buddy!
```
Would be the following:

```bash
Hello there Bobby
Hello there buddy!
Hello there Bobby buddy!
```
4 changes: 2 additions & 2 deletions ebook/en/content/005-bash-user-input.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bash User Input

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

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:

Expand Down Expand Up @@ -51,4 +51,4 @@ echo "Hi there $name"
echo "Welcome to DevDojo!"
```

Make sure to test this out yourself as well!
Make sure to test this out yourself as well!
8 changes: 2 additions & 6 deletions ebook/en/content/007-bash-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can pass arguments to your shell script when you execute it. To pass an argu
./devdojo.com your_argument
```

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

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

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

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

rm -f $0
rm -f "$0"
```

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




Loading