What is the Meaning of $0 in the Bash Shell?

In the Bash shell, $0 refers to the name of the executing shell script or program. It is a special variable that holds the name of the shell or script that is currently being executed from the command line. This variable can be beneficial within a script or shell program, as it allows users to access the name of the program being run, which can be used in various ways, such as displaying usage instructions or error messages.

This article will illustrate the meaning of $0 in the bash shell. 

Meaning of $0 in the Bash Shell

The variable “$0” is utilized to represent the name of the current shell script or the currently running program. The number 0 typically represents a successful exit status of a command or script. When users run a command in the Bash shell, check its exit status code by running echo $?. If the command is successful and returns an exit status of 0, you will see 0 printed on the screen.

How Does $0 Work in Bash Shell?

Here are a few examples of how $0 works in Bash:

Example 1: Displaying the Name of the Current Shell Script

An example is considered to display the name of the current shell script. For this, open the script.sh file via nano text editor as below:

$ sudo nano script.sh

After executing the above code, it navigates to the nano text editor:

#!/bin/bash
echo "The name of this script is: $0"

In the above script, the “$0” variable refers to the name of the current script file. Let’s execute the script:

$ ./script.sh

Save and close the script file.

Run the script by mentioning the file name below:

The output shows that “The name of this script is: ./script.sh” in the terminal.

Example 2: With Different Command-Line Arguments

Let’s use the “$0” with the command line arguments in the following code example:

#!/bin/bash
a=10;
b=15;
echo "The first argument is: $a"
echo "The second argument is: $b"
echo "The name of this script is: $0"

In this example, $a and $b represent the first and second command-line arguments having predefined values 10 and 15 that are passed to the script. The “$0” is utilized to display the name of the script:

Save and close the script file. Then, execute the ./script.sh file in the terminal:

The output shows that the value of the first and second arguments 10 and 15 are displayed with the file name script.sh. 

Example 3: Using $0 in the Bash Function

Another example is carried out to use the $0 in the bash function. Let’s check the example as below:

#!/bin/bash
func()
{
add=$(($1 +$2))
echo "Result is :" $add
echo "Name of the script is:" $0
}
func 10 5

In this example, func() is utilized to add two arguments values 10 and 5 in $1 and $2 and store the result value in the “add” variable. After that, display the result of the “add” variable and the “$0” via the “echo” command. 

Save and close the script file. After that, execute the ./script.sh file in the terminal:

$ ./script.sh

The output displays the “15” and name of the script “script.sh” in the terminal.

Conclusion

In a Bash shell, $0 is a variable that represents the name of the current shell script or running program. Users can use it to handle command-line arguments, display script information as part of a message or log output and many more.

This article has explained the meaning of $0 along with different examples in a bash shell.