| by Arround The Web | No comments

What is $0 in a Bash Script?

Bash is a command interpreter and script. Like other forms of coding, it supports packet forwarding, variables, and functions. A script is a file with commands that the software may view and perform. In Linux, bash gives us positional parameters like “$0,” “$1,” “$2,” “$3,” etc. In this article, one of its positional parameters, “$0,” will be used.

Example # 1: Using $0 in a Bash Script

The name of the terminal or shelled script is expanded using “$0”. At shell initialization, this is set. $0 is assigned to the name of that directory if Bash is called with a collection of commands. To put it simply, we utilize “$0” in bash to store the name of the script and display it in the terminal. The parameters for the bash command on Linux are “$0,” “$1,” and “$2, etc. The first one stores the name of the script, the second one stores the first value, and so on.

Now, in this section, we will use the “echo” command to display the name of the bash shell. To do this, we must execute the command on the terminal, but first, we must create a bash file. Since one already exists and is located on the desktop, we will utilize it in this article. Its name is “code.sh.”

To print the name of the script on the terminal when we open this file, we must first add the shell, which is “#!/bin/bash,”. In the next line, we must pass the parameter “$0” within the echo command because the file name is stored in this parameter.

#!/bin/bash

echo $0

We will now execute the command by first entering the term “bash” and then the name of the script, “code.sh.”

Linux@linux:~/Desktop$ bash code.sh

As shown in the screenshot below, when we run this command, the name of the script, “code.sh,” is displayed.

The “$0” option in bash will now be used in the following section to display the current name of the script together with the statement. To accomplish this, we will create a script in which, after adding the shell as described above, we use the “echo” command to print the statement, “The name of the file is:.” The statement that we wish to print needs to be typed with an inverted comma before we pass the “$0” variable, which stores the script name.

#!/bin/bash

echo “The name of the file is :” $0

The command that we will now use is “bash,” followed by the name of the script, “code.sh”. When we execute this command, the input string and the script name, which is saved in “$0,” will both be displayed.

Linux@linux:~/Desktop$ bash code.sh

You can now see in the image below that the file name for the script, “code.sh,” is displayed along with the echo statement, “The name of the file is”.

Example # 2: Displaying the Script’s Name at the End by Using Bash’s “$0”.

Now, in this section, we are going to use “$0” in the script’s end section, which will display the name of the bash file at the end. For this, we are going to construct a script in which we first use the bash shell, which is “#!bin/bash,” and then we initialize the variables “x,” “y,” and “z,” assigning values to each of them “6” for “x”, “4” for “y” and “9” for “z”. Then we will use the echo command to print these values in the following. To do this, we pass the “$” parameter along with the variables that contain the numbers, so we use “$x,” “$y,” and “$z.” This will display the values in the terminal when the bash file is opened using the command in the terminal.

Then, in the following line, we once more use the “echo” command to print the statement and the script’s name using the argument “$0.” Since the script’s name is stored in the file, the statement we need to print is “The name of the file is:” and we do this by passing “$0” as an argument.

#!/bin/bash

X=6

Y=4

Z=9

echo$x” “$y” “Sz”

echo “The name of the file is :” $0

The command to open the script output in the terminal is now used.

Linux@linux:~/Desktop$ bash code.sh

Now that this command has been executed, you can see in the image below that it first displayed the values “6”, “4”, and “9” before displaying the statement “The name of the file is” and the file name “code.sh” in the line that follows.

Example # 3: Using $0 in the Bash Function

In this section, we will use the bash function to subtract two numbers and print the name of the relevant bash file by placing a “$0” inside the function body. To do this, we will develop a script in which we create a function called “func()”. Because it is a user-defined procedure, we are free to choose its name. Then, in the following line, we use curly brackets to form the function’s body. Within these brackets, we use the variable “sub,” in which we store the subtraction of two numbers by using the “$” parameter.

Next, we use double-round-brackets. Within these brackets, we perform the subtraction of two numbers. Therefore, we use “$1-$2”.  The number we specify when invoking the function “func” will be stored in its “$1” and “$2” parameter values. Next, we use the echo command to print the statement and the answer by using the variable “sub” with the argument “$.” Because the result of subtraction is stored in this.

Next, we use the echo command to print the statement and the name of the script by using “$0” the statement is “The name of the script is,” and we pass the argument “$0. Then, in the following line, we call the function by using its name, “func,” and supply two numbers as arguments, “50” and “30,” which will be stored in “$1” and “$2,” before performing the subtraction operation.

#!/bin/bash

func()

{

Sub=$ ( ( $1 -$2 ) )

echo “The answer is : $sub

echo “The name of the script is:” $0

}

func 50 30

To display the result in the terminal, we will now use the same command as in the previous section.

Linux@linux:~/Desktop$ bash code.sh

Now, it displayed the result of subtracting the given numbers, which is “20,” with the statement “The answer is,” and in the following line, it displayed the statement “The name of the script is,” along with the name of the bash script, “code.sh,” which is displayed by supplying the parameter “$0” to bash.

Conclusion

This article has discussed using “$0” in Linux bash scripts. Because the file name is stored in this “$0,” we have utilized it in various cases and shown the bash script’s name in the terminal. In the first section, the script’s name is simply printed using the echo command and the “$0”. In the second section, we used “$0” to display the script’s name at the end, and in the final example, we utilized the bash function() to do a subtraction operation while also using “$0” in the function’s body.

Share Button

Source: linuxhint.com

Leave a Reply