1. Basic variables in Bash
Let's learn how to create some basic variables in Bash scripts.
2. Assigning variables
Assigning variables is similar to other languages. You can assign the variable name using the equals notation.
Here we make a variable 'var1' and assign the string 'Moon' to be its value.
Then, you can reference that variable with the dollar sign notation.
3. Assigning string variables
You can name the variables as you like and it is good practice to name them something which makes sense.
Let's create variables for a person's first and last name
and then echo out using the dollar sign notation.
Nice! We were able to access and print both variables.
4. Missing the $ notation
The dollar-sign notation is very important, otherwise Bash doesn't understand you are referencing a variable.
Here we create the same variables but don't reference with the dollar sign notation.
Bash just thinks we are echoing out three strings, not variables.
5. (Not) assigning variables
Bash is not as forgiving as other languages. You must not add spaces around the equals sign.
See here we add some spaces in the variable assignment.
When we run the script, there is this error.
6. Single, double, backticks
In Bash, using different types of quotation marks can mean different things, when creating variables and printing.
Single quotation marks means that whatever is between them is interpreted literally.
Double quotations is similar to single quotation marks, but it will understand dollar-sign notation and backticks that occur between the quotation marks.
The last way is quite special and powerful. Backticks creates a shell within a shell.
That is, it sends whatever is between the backticks out to a new shell and takes the results back into the variable.
Let's see these methods in action.
7. Different variable creation
Let's see the effect of different types of quotation marks for variable creation.
Firstly we create a variable called now_var and assign the string NOW.
We then create a variable using single quotes around a dollar-sign notation call to this variable. You can see that the use of single quotes treats what we gave it literally, it doesn't understand there is text inside that variable.
We also create a variable with double quotes in the same way. the double quotes understands our variable call and returns the NOW text inside that variable.
8. The date program
We will use the date program to demonstrate the use of backticks for variable creation.
The normal output of this program is as follows.
In terminal, type the word date.
You will get the current date and time returned as a string.
9. Shell within a shell
Now let's use the powerful shell-within-a-shell construct.
We create a variable similar to before. However, instead of using single quotes, we use use backticks around the word date and echo the resulting variable.
We can see the result includes the current date and time.
What actually happened here is that a mini shell was opened between those backticks and called the date program. The result of this call was then saved back as a string in-line.
Congratulations - the shell-within-a-shell is a powerful and useful construct to add to your toolkit!
10. Parentheses vs backticks
There is an alternate way to invoke a shell-within-a-shell.
This uses a dollar sign around parentheses as can be seen here. We create two variables around the date command and echo them both.
You can see the result is exactly the same.
Either method works. The backticks method is more backwards compatible as it is older. Though the parentheses method is more modern and has some advantages, discussed in the link here.
11. Let's practice!
Let's practice creating and using variables in Bash scripts!