BASH Programming

How to Use Special Variables in Bash

In this article, we are highlighting the use of special variables in Bash. It is a file that may be used to process commands that can be entered into a terminal window. This post includes multiple commands for execution. The definition of special variables and their purpose will be covered first.

What are Special Variables in Bash?

Special variables are the ones whose meanings are predefined. They are deployed to manage the control flow of the Bash script.

Getting their values is possible but providing values to them is impossible.

Bash vs Shell

A computer software known as a “shell” receives commands, interprets them, and then sends them to the operating system for processing. Most operating systems built on Linux have at least one shell software. Dash, Bash, or both will likely be the shell application.

Bash is easy to use and less portable than shell scripting

Shell script usage among the users is more likely to increase the longer they use Linux. You should urge them toward using shell scripting at first to perform easier jobs before moving to traditional development once they run into an intractable brick wall. Shell scripts are an excellent approach to begin programming because they are so easy to understand and develop.

Examples of Special Variables in Bash:

Here are some examples of special variables in Bash.

Variable Purpose
-$* Dollar star, Stores the whole parameter at a single string
-$# It is several arguments specified in the command line
$0 This represents the name of the file
$1 It represents the first argument
$2 This is the second argument from the command line
$@ It is used to each string treated as a separate argument
$? This reveals the last command’s exit status.
$$ This gives the process ID of the current shell
$! This shows the PID of the last background job done
$ This shows the PID of the last background job done
USER the user whose script is being run by their username

Now, we can use some of the examples of special variables and see how we can run them in our programs.

Example of $*:

This example is for all levels of developers. Here, we will see how and where we can use the special variable $*

echo “This is my ” “ Bash Article”; $*

In this above example, we write two separate sentences in double quotes and at the end of this sentence, we add a semi-colon (;) and $*(Dollar*). As a result, we get a single sentence this means Bash acknowledge this as a single quote.

Here is the result:

This is my Bash Article

Example of $0:

$0 is a special variable of bash that shows the file name of the script that is running in your terminal, for example, if we type:

echo $0

So, in this above example when we type $0, as a result, we get an output of main.sh. Therefore, main .sh is our file name.

main.sh

Example of $?:

$? is a special variable of bash that provides the exit code of the last command of the terminal. Knowing the most recent command will facilitate the user to continue with their script.

So, in the below code if we type “echo $?”

echo $?

The output is given below:

0

Now that we have typed $? and if we received a result of 0 in our terminal, it meant that the current program had been properly completed. However, if we received a result of 1 or more like (123456789), it meant that there was a programming problem, or we got an error.

Let us understand the above statement with a code snippet provided below.

rm “my file name”

The result is shown here.

0

rm: cannot remove ‘my file name

: no such file or directory

We got 0, but in the next code, we got an error (code 1) rm: cannot remove ‘my file name No such file or directory. So, we don’t have any file named, “My file name” in our directory, which is why we received an error as [execution complete with the exit code 0]

Example of $!:

This $! (Dollar exclamatory) is a special variable of bash that provides the PID (process ID) of the most recent background job done.

We cannot assign value to them only references are accepted for them.

Let us examine the statement above in our coding terminal with the following code:

echo $!

Our process ID of the recent background job can be seen in the output as whatever number we receive as a result. In the output, it says [Execution completes with exit code 0].

Example of $-:

The dollar hyphen ($-) is a special variable that provides the flags present in use by the Bash shell.

Let us see the example and its output in our terminal.

echo $-

Our present Bash shell has the -h and -b, flags available in our terminal

hB

Flags are used to set up options and arguments for the commands you run.

Example of $$-:

The $$ returns the PID number of the active shell. Regardless of whether you employ a specific Bash variable from the Linux command prompt or in the shell script, this has varying effects. This is so because the active bash shell’s process ID is generated by $$. However, a new Bash shell is launched when you start a new script.

Let us use the following code to test this out.

echo $$

In the output given below, we got 36 as our PID.

36

Now we can see the combined example of special variables and their outcomes so that we can understand it in one interface.

echo “ Username is: $ USER”

echo “ Computer hostname is: $HOSTNAME

echo “Random number is $RANDOM

echo “The process Id is:$$

In the snippet, we used $USER, $HOSTINGNAME, $RANDOM and $$. As a result, we have the output of hostname, random number, and our process of ID (PID) we did not provide a user name, so it is showing nothing to our users.

Username is:

Computer hostname is: 169.254.212.113

Random number is 20057

The process Id is:125

Conclusion

We covered bash scripting, numerous bash special variables, how and where to apply them, and how to execute programs in it. Each of these variables is unique from one another and acts differently. Additionally, we offered examples of how to use them in the bash terminal and provide their syntaxes as well, for explanation and provide more clarity to beginners.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content