Use Scenarios of $_ in Bash in Ubuntu 20.04:
There are distinctive use cases of employing the exclusive variable “$_” in Bash in Ubuntu 20.04 however, down below, we will be discussing the 3 most frequent use conditions of this special variable.
Use Circumstance # 1: Working with “$_” in Ubuntu 20.04 Terminal:
This particular variable can be made use of in the Ubuntu 20.04 terminal. The goal of using it inside of the terminal is to print the last argument of the preceding command executed within the terminal. Consider a scenario in which you executed a command some time back and did not execute nearly anything in your terminal immediately after that, but you even now want to know what you did previous time. In this problem, you can use the “$_” particular variable in the terminal to know about the very last argument of the prior command that you executed in your terminal. To have an understanding of all this in a far better way, you can take a appear at the adhering to instance:
In this illustration, we intend to print the last argument of the earlier executed command in the Ubuntu 20.04 terminal. For that, we have executed the command shown underneath in our terminal:
The command talked about earlier mentioned is in essence an integration of two distinct commands. The first command, i.e., ls *.sh, lists down all the Bash data files existing in just the present-day listing. On the other hand, the second command, i.e., echo $_ will show the final argument of the “ls” command, i.e., whichever will be printed past as a end result of executing the “ls” command will be printed once more when the “echo $_” command will be executed. The output of this command can be seen from the following impression:
In this output, you can obviously see that the very last argument printed due to executing the “ls” command is the Bash file whose name is “Suppress.sh”. Furthermore, you can also see that the exact same file title is printed all over again simply because of making use of the “$_” variable because this file was, in reality, the final argument of the beforehand executed command in the terminal, i.e., the “ls” command.
Use Circumstance # 2: Utilizing “$_” for Displaying the Path of the Bash Script:
The “$_” specific variable can even be made use of for exhibiting the path of a Bash script in Ubuntu 20.04. It can do so if you generate a simple Bash script and use the “$_” unique variable just before creating any other command in your Bash script. By doing so, you will be ready to get the route of your Bash script pretty quickly. To display the functionality of this special variable in Bash, we have created the case in point shown down below. Just go via this instance for at the time, and you will be able to straight away know how the “$_” distinctive variable can be utilized to show the route of the Bash script.
Case in point:
In this illustration, we want to use the “$_” special variable for displaying the path of a Bash script on the Ubuntu 20.04 terminal. For executing so, we have developed the following sample Bash script and named it “Sample.sh”:
In this Bash script, right after crafting Shebang, we have just used the “echo $_” command so that when we execute this Bash script, it will print the worth of the “$_” exclusive variable on the terminal, i.e., the path of our Bash script file. You can also lengthen this script more by introducing more instructions of your preference immediately after the “echo $_” command.
To execute this Bash script, you will have to run the command shown underneath in your terminal:
When you execute this Bash script, its route will be printed on your terminal as a consequence of applying the “$_” distinctive variable in your Bash script, as proven in the subsequent graphic:
The route of the Bash file that we made in our circumstance was /bin/bash, as you can see from the output revealed in the picture previously mentioned.
Use Circumstance # 3: Applying “$_” for Displaying the Previous Argument of the Prior Command in a Bash Script:
This use circumstance is fairly identical to the first use situation of our write-up. However, in the to start with use circumstance, we have only utilized integration of two instructions in just the Ubuntu 20.04 terminal, whereas in this use case, we will build a Bash script that will provide extra or a lot less the exact same objective, i.e., in this Bash script, we will use the “$_” special variable following some instructions in a way that it will print the very last argument of the former command on the terminal. To grasp this thought in a far better way, you want to go through the illustration that we have produced down below:
In this example, we have established a sample Bash script named “Sample.sh” and immediately after stating Shebang, we have declared two variables “a” and “b”. We have also assigned the values of “10” and “12” to these two variables, respectively. Immediately after that, we have utilised the “echo” command to print the values of these two variables. At last, we have used a further “echo” command to print the worth of the “$_” exclusive variable, which in this scenario will be the last argument of the beforehand executed “echo” command, i.e., the benefit of the “b” variable.
Following building this Bash script, you can run it with the assist of the adhering to command:
When managing this sample Bash script, you will see the benefit of the variables “a” and “b” on the terminal. In addition to that, the benefit of the “b” variable will also be printed again due to the fact of the “$_” distinctive variable as shown in the impression down below:
This tutorial shed light on the three most popular use circumstances of the “$_” particular variable of Bash in Ubuntu 20.04. With these use situations, you can either use the “$_” special variable inside the system’s terminal, or you can even generate Bash scripts for utilizing this distinctive variable. Additionally, you can even boost the complexity of the Bash scripts that have been shared with you in this report. The goal of this report was to give you an overview of the usage of the $_” exclusive variable in Bash in a Ubuntu 20.04 process.