Variables in Bash/Shell Scripting

Let’s take a look at defining and using a variable in Bash. We will even look at some unique variables that you will find very handy. Heads up! This post is a continuation of the Bash Scripting Basics post we did earlier. Be sure to check it out!

What is a Variable?

A variable is a symbolic name that has a value associated with it. Its name is variable because its value varies. We use variables to store values for future use.

How to define and use variables?

We can define bash variables in a terminal or script in the following ways:

number=5
my_str='Hello'
str2="How are you?"

There should be no space before, and after the assignment operator ( = ). You will get an error otherwise.

Also, you can use both single quotes and double quotes while defining the string variables as we did above.

We can use the dollar symbol syntax to get the values associated/stored in the variables:

echo $number
echo $my_str
echo $str2


Moreover, it is a good idea to get the value of a variable by surrounding it in double-quotes like below:

echo "$number"

Example:


Let’s see the working of bash variables in a script:

#!/bin/bash
number=5
my_str='Hello'
str2="How are you?"

We will get the following output when we run the script:

5
Hello
How are you?

Now let’s understand more about parameters/arguments.

What is a parameter/argument in a script?

A parameter/argument is a value that we want to use in the script. Let’s say you want to use a value 3.14 in your script. One way is to declare a variable in your script and assign this value to that variable.

Another way would be to pass this value as an argument to the script. An argument value can be a number, a string, or it can also be a variable.

How to pass an argument/parameter to a script?

Let’s say we have a script named as script.sh and we want to run this script by-passing the argument value 3.14.

This is how we will do this:

bash script.sh 3.14

We can also pass many arguments in the following way:

bash script.sh 3.14 "hello"

Do not forget to use a space between the arguments. We can also pass an argument containing spaces in the following way:

bash script.sh 3.14 "Scripting is Fun" hi

As you can see, we are passing three arguments which are all separated by a space. Bash considers anything between the double-quotes as one argument. It is a good practice to pass the string arguments by surrounding them with double-quotes. For instance, we could also surround hi in the above script call with double quotes like “hi”.

Moreover, we can also pass the arguments in the form of variables. Let’s say have a variable named as pi, and it contains the value 3.14.

We can pass this variable in the following way:

bash script.sh pi

Next, we will learn how we can use these arguments in a script through the use of special variables.

Special Bash Variables:

The shell defines these very useful variables. We only use them to collect the arguments passed to the script, and we do not have to set them first.

$0

Returns the name of the currently executed script.

If you write the command echo $0 in your script and let’s say you run the script like this: bash script.sh. Then it will return script.sh, which is the name of this script.

$#

Returns the count of arguments passed to the script.

If you write the command, echo $# in your script, and let’s say you run the script like this: bash script.sh hello hi. Then it will return two since you passed two arguments: hello and hi.

$n

Here n is a variable that can have any value starting from one.

If the value of n is one, then it returns the value of the 1st argument passed to the script, e.g. echo $1. If the value of n is two, then it returns the value of 2nd argument passed to the script, e.g. echo $2. So, in short, you can get the value of any argument by specifying the value of n.

${@: -1}

Returns the last argument passed to the script.

If you write the command echo ${@: -1} in the script, and say you run the script like this: bash script.sh hello hi. Then it will return hi, which is the last argument.

$@

Returns all the arguments passed to the script.

If you write the command, echo $@ in the script, and let us say you run the script like this: bash script.sh hello hi how. Then it will return hello hi how which are all the arguments.

$?

Returns the exit status for the last executed command where 0 (zero) means success.

Let’s say you write the command echo $@ on the 5th line of the script. Then it will return the exit status for the command which is at the 4th line. Remember, you can also run this command in the terminal to check the status of the last command executed

$$

Gives the process ID of the currently running instance of the bash shell.

In Summary

We have learned the basics of setting and using variables in Bash. There is a lot more to learn about this subject, but this post will help you get by. Want to know more? Why not read the Bash manual for more in-depth information.



About the Authors

Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *