Introduction
A variable is a place in memory where a value is kept. This place has a name to make it more descriptive. Any sort of data, such as name, number, text, filename, or directory, could be the value. A variable is, therefore, nothing more than a reference to a specific piece of data. A user can create, assign, or delete variables using a shell. These variables, however, are just momentary and are immediately erased when the shell session ends. A shell variable must be exported to become an environment variable and become persistent and system-wide.
Additionally, Linux has unique variable features. They are used to manage the execution flow of Linux scripts. Their values can be read, but they cannot have values assigned to them. This blog will teach you how to use nine special variables in Linux. So let's investigate today's subject of unique Linux variables.
Special Variables in Linux
$$
The process ID or PID number of the currently active shell is provided by the $$. This behaves differently depending on whether you use this particular Bash variable from the Linux command line or within the shell script. This is because $$ generates the process ID of the active bash shell. However, a new Bash shell is launched when you start a new script.
Let’s take an example to better understand it.
This gives the PID of the current shell.
$@
All parameters given to the Bash script are referred to as $@ (dollar at the rate). Every argument receives a double quotation. For instance, the $@ variable represents $1 and $2 when a Bash script is given two parameters.
The first parameter would be connected with the first portion of the word, and the last parameter would be merged with the end part of the entire word. Each positional parameter extends as a separate field. The expansion of the special variable @, even when you double-quote it, results in zero areas if there is no positional argument.
In brief, $@ expands to the arguments passed from the caller to a function or a script. Its meaning is context-dependent: Inside a function, it expands to the arguments passed to such function. If used in a script (outside a function), it expands to the arguments passed to such script.
#!/bin/bash
# special.sh
echo "Process ID of shell = $$"
echo "Program name = $0"
echo "Number of args = $#"
echo "Argument 1 = $1"
echo "Argument 2 = $2"
echo "Complete list of arguments = $*"
echo "Complete list of arguments = $@"
$* and $@ are used to get the total number of arguments in the script, and we currently assign 3 arguments, therefore it is printing the arg1, arg2, arg3.
$*
All variables written as a single string are denoted by the special variable $* (dollar star). The majority of arguments are double-quoted.
In the above example, we enclosed two distinct pieces in double quotations; (semicolon). Bash combined the two components into a single argument. Bash recognizes the space you type in a Bash client as a separator.
Starting a Bash script allows you to pass arguments. The script deals with the arguments supplied to it. For whatever reason, declaring or not declaring many variables to the Bash script has no effect if the script doesn't handle disputes. $1, $2, and other variables in passing arguments.
$#
In Bash, a unique variable called $# (dollar hash) expands to the decimal value of the positional parameters. The Bash script or total shell number of parameters is contained in the variable $#. Use the following syntax when parameters are given directly.
bash -c 'echo $#' _ <arg1> <arg2>...
In the example above, Bash -c receives an argument written after a command. Here the _ (underscore) denotes a placeholder.
Note: The command name (parameter 0) is not included in the number shown by the '#' symbol. This is so that it is clear that "#" is a unique, not a positional, parameter.
#!/bin/bash
echo $0
echo $1
echo $2
echo $#
We first take one argument, and then two argument and then we use $# therefore it print 3, as we have total 3 arguments in the script.
$0
The running script's filename is displayed using the special variable $0. In this case, type:
echo $0
This produces the following output.
The output shows “bash” as our current script’s filename.
$?
A unique variable called $? shows the exit code of the most recent command. Once you are aware of a statement's exit code, you can move the script in a number of different directions. Exit codes of zero often indicate that the previous process was terminated successfully. If the exit code is 1 (or more), it frequently denotes that the process came to a halt due to a mistake or a poor result. The order is:
Echo $?
Let’s discuss an example to better understand
I successfully ran my final piece of code, so when I ran the command, the output was 0. Then we received the message "rm: can't remove coding. world': No such file or directory." The outcome of this after running the program was 1. Here, we attempted to use the rm command to remove the file "coding.world". But it doesn't appear like coding.world is already present in our shell. This is why we got an error.
$!
A unique variable called $! (dollar exclamation) holds the PID of the most recent job that has been backgrounded. extends to the process ID of the most recent background or asynchronous command. Some parameters are handled differently by the shell. These cannot have values assigned to them; only references are permitted.
The syntax for using the variable
echo "$!"
$! Holds the PID of the most recent job, hence PID is 10492 of the recent job, and when we print it using $! we get the same output
$-
The unique variable $- (dollar hyphen) returns the flags currently utilized in the Bash shell. The shell's active terminal flags are contained in $-. These flags control the behavior of your shell.
Here is the syntax and its result.
echo $-
We can see -h, -i, -m, -B, -H, -s flags are active in our current Bash shell. Below are some flags and their meanings.
- -s: The abbreviation for stdin is -s. This reads stdin commands.
- -m: The abbreviation for monitor is -m. This makes job control possible.
- -i: The abbreviation for interactive is -i. It indicates that the active shell is interactive.
- -n: The abbreviation for noexec is -n. It implies that you cannot execute script commands; you can only read them.
- -a: The abbreviation for allexport is-a. All declared variables are exported using this.
- -D: -D does not allow you to run script commands; instead, it displays a list of all double-quoted strings with a $ prefix.
- -C: Through redirection, it stops you from overwriting files.
- -B: This makes Bash's brace expansion feature available.
$_
$_ (dollar underscore) is a special Bash variable set to latest argument of the last executed command.
As for the syntax:
bash -c 'echo $#' _ x y
echo $_
You can see from the example above that we passed the two arguments, x and y. Therefore, y is the final parameter of the most recent command to be executed. As a result, when we executed the $ variable, the result was y.