Skip to content
Merged
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
30 changes: 15 additions & 15 deletions ebook/en/content/004-bash-variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bash Variables

As with any other programming language, you can use variables in Bash as well. However, there are no data types, and a variable in Bash can contain numbers and characters.
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:

Expand All @@ -10,29 +10,29 @@ 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 like this:
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 good practice, and I would advise to use them whenever you can:
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 would output: `DevDojo` as this is the value of our variable.
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.
Next, let's update our `devdojo.sh` script and include a variable in it.

Again, with your favorite text editor, open the file:
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
```

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

```bash
#!/bin/bash
Expand All @@ -42,7 +42,7 @@ name="DevDojo"
echo "Hi there $name"
```

Save it and run it again:
Save it and run the file using the command below:

```bash
./devdojo.sh
Expand All @@ -54,13 +54,13 @@ You would see the following output on your screen:
Hi there DevDojo
```

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

* `#!/bin/bash` - 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 by using `echo`
* `#!/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:
You can also add multiple variables in the file as shown below:

```bash
#!/bin/bash
Expand All @@ -71,7 +71,7 @@ greeting="Hello"
echo "$greeting $name"
```

Save it and run it again:
Save the file and run it again:

```bash
./devdojo.sh
Expand All @@ -83,4 +83,4 @@ You would see the following output on your screen:
Hello DevDojo
```

Note that you don't necessarily need to add semicolon `;` at the end of each line. It would work both ways, a bit like in JavaScript!
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!