Usage of $ in a Bash Script in Ubuntu 20.04:
$ belongs to a various classification of exclusive variables in Bash, also known as positional parameters. These parameters assortment from $ to $9, and as their identify implies, these variables correspond to diverse values within a Bash script dependent on their positions. As considerably as the $ specific variable by itself is concerned, this exclusive variable serves two distinctive applications i.e. it can either print the identify of a Bash script or refer to the name of your latest shell.
Now you might be asking yourself how this variable can correspond to two various values at the exact same time. Well, the answer to this issue is very easy. This variable does not correspond to two values concurrently instead, dependent on where this variable is utilized, it can refer to possibly of those two values. If the $ specific variable is utilized in just a Bash script, it can be utilized to print its title and if it is utilised specifically within just the terminal, it can be utilized to exhibit the title of the present shell.
On the other hand, in this write-up, since our main issue is with employing the $ particular variable inside of a Bash script in Ubuntu 20.04, for that reason, you will have to go by means of the next area to test out some relevant examples.
Illustrations of Working with $ in a Bash Script in Ubuntu 20.04:
In the subsequent examples, we will be utilizing the $ particular variable in a Bash script at three unique sites. Our target is to see if its output differs by modifying its placement or not. To find out, you will have to discover the 3 examples discussed underneath:
Instance # 1: Using $ at the starting of a Bash Script in Ubuntu 20.04:
For the very first illustration, we have made a incredibly easy Bash script that can be observed in the subsequent graphic:
In this Bash script, we have just published an “echo” command to print the $ particular variable value on the terminal down below the Shebang (which is obligatory to detect a Bash script). Due to the fact we have utilised the $ exclusive variable within just a Bash script, it will definitely refer to the identify of our Bash script. To validate this, we will execute our Bash script with the subsequent command:
Listed here, Temp.sh was the identify of the Bash script file that we produced. You will have to change it with the identify of your unique Bash file.
When this individual Bash script is executed, you will be capable to see the identify of your Bash script file printed on your Ubuntu 20.04 terminal as shown in the picture underneath:
Illustration # 2: Utilizing $ in the middle of a Bash Script in Ubuntu 20.04:
For this case in point, we have expanded the same Bash script a tiny even further than we have employed in our initial illustration. The goal of this modified Bash script was to use the $ special variable someplace in the center of the script to determine out if its functionality differs from that of the 1st instance or not. This modified Bash script can be viewed from the next graphic:
In this Bash script, we have declared a few variables, “a, b, and c” and assigned them the values “10, 20, and 30,” respectively. Soon after that, we have applied the “echo” command to print the values of the variables “a” and “b” on the terminal. Then, yet another “echo” command will endeavor to print the benefit of the $ distinctive variable. Last but not least, there is still one more “echo” command that will print the variable “c” worth on the terminal.
This Bash script file can also be executed likewise as we did in the very first case in point. On execution, the output rendered by this modified Bash script is demonstrated in the impression under:
From this output, you can see that this Bash script has initial printed the values of the variables “a” and “b”, then it has printed the value of the $ particular variable, i.e., the name of the Bash script adopted by the worth of the variable “c”. It indicates that even when the $ distinctive symbol was made use of in the middle of the Bash script, it nevertheless contained the very same value as it did in the very first illustration.
Illustration # 3: Making use of $ at the conclude of a Bash Script in Ubuntu 20.04:
This case in point is but a different modified variation of the to start with Bash script. In this Bash script, we intended to use the $ specific variable at the conclusion of the Bash script to see if its doing the job differs from that of the initial example or not. This modified Bash script file is demonstrated in the next graphic:
In this Bash script, we utilized the similar three variables we had in the second example. Then we have utilised an “echo” command to print the values of all of these variables, followed by another “echo” command that will attempt to print the worth of the $ particular variable, i.e., the title of our Bash script file.
This Bash script file can also be executed equally as we did in the first case in point. On execution, the output rendered by this modified Bash script is proven in the impression under:
This output shows that this Bash script has initially printed the values of the a few variables followed by the worth of the $ unique variable, i.e., the identify of the Bash script. It implies that even when we applied the $ unique variable at the finish of a Bash script, it continue to held the identify of the Bash script file in it.
Use of $ in the Terminal in Ubuntu 20.04:
This is just an more utilization of the specific variable under dialogue. The $ exclusive variable can be made use of in the terminal to print the identify of your current shell only by executing the following statement:
Considering that our present shell name was Bash, it is also evident from the output of the statement mentioned previously mentioned.
From this post, we can conclude that the unique variable $ can provide two different reasons i.e. for printing the title of a Bash script and printing the title of the present-day shell. Furthermore, we also located out that no matter of the placement of the $ exclusive variable inside of a Bash script, it will normally maintain the name of that Bash script as its worth.