List of Special Parameters in Bash with Examples

Linux TLDR
Last Updated:
Reading time: 4 minutes

In this article, you will learn what the difference is between variables and parameters, what special parameters are, and a list of predefined special parameters in Bash.

What are β€œSpecial Parameters” in Bash?

Before you can understand what special parameters are, you must first understand what parameters are: Any entity that stores a value (also known as a variable) is the parameter.

Yes, it’s that short in definition; now, special parameters are a few predefined parameters given by your shell that hold read-only information related to your existing shell session.

This parameter can be used in your terminal or shell script to control how the script runs by figuring out the user’s shell session or getting the arguments given to the script by user.

List of Bash Special Parameters

The following is the list of known predefined special parameters served by your shell (basically Bash).

Special ParametersDescription
$$Return the PID of an existing Bash shell.
$!Return the PID of the latest executed command.
$#Count the number of positional arguments specified to the script in decimal.
$0Print the script name.
$?Display the exit status of the last executed command.
$_Return the last argument specified to the script.
$iThe β€œi” refers to the position of the argument; for example, the β€œ$1” parameter returns the first argument, and the β€œ$2” parameter returns the second argument specified to the script.
$@Return each of the arguments passed to script separately.
$*Identical to the β€œ$@” parameter, except that when specified inside quotes, it treats all arguments as a single argument.
$-Return the current flag assigned to your Bash shell.

Prerequisite

Few of the special parameters can be shown in the terminal, but some must be shown in the shell script for you to fully understand how to use them.

For that reason, check out our article on how to run shell scripts in Linux to create scripts when they are required.

Displaying the PID of an Existing Bash Shell

The β€œ$$” parameter holds the PID information of your existing bash shell; for example, when you echo the value of this parameter, you will find the PID of your existing bash shell.

Displaying the PID of an existing bash shell

Note that when the β€œ$$” parameter is used inside the script, it will return the PID of the script (executed in a subshell) itself.

Displaying the PID of the Last Executed Command

Unlike the β€œ$$” parameter, the β€œ$!” parameter returns the PID of the latest executed command in your current shell session.

The following command will return the PID of the sleep command in the output.

Displaying the PID of lastest executed command

Counting the Number of Arguments Assigned to Shell Script

The β€œ$#” parameter is mostly used inside the shell script to count the number of arguments assigned to the referenced shell script at the time of execution.

The following is the simple script that echoes the β€œ$#” parameter when executed.

Echoing the "$#" parameter

If the user specified two arguments while running the above script, the β€œ$#” parameter will return 2 in the output.

Counting the assigned arguments to shell script - example1

If the user specified five arguments while running this script, the β€œ$#” parameter will return 5 in the output.

Counting the assigned arguments to shell script - example2

Printing the Referenced Script Name

The β€œ$0” parameter is used to print the name of the shell or referenced shell script. As a result, you can use this to print the name of the shell script.

The following is the simple shell script echoing the β€œ$0” parameter, when script successfully executes.

Echoing the "$0" parameter

When the above script is executed, it will return the name of the script itself.

Printing the name of the shell script itself

Note that when you use this parameter in the terminal, it will return Bash, as this is the first process before your command is used to run.

Printing the first process in shell

Displaying the Exit Status of the Last Executed Command

The β€œ$?” parameter is used to get the exit status of the most recently executed command in the current shell session; we have already written a detailed guide on this topic that you can check.

But for your reference, check the following list:

  • 0: Successful execution.
  • 1: General error.
  • 2: Shell built-in errors.
  • 127: Command not found.

In the following example, one is the right command that was executed successfully, returning β€œ0” as the exit status code, and another is the wrong command that failed to execute and returned the β€œ127” exit status code.

Displaying the exit status of a recently executed command

Note that this parameter is a crucial part of the shell scripting process that is used to control the flow of script in different situations based on the last exit status code.

Displaying the Last Argument Provided to the Previous Command

The β€œ$_” parameter will display the last argument (or positional parameter) assigned to the previous command, whether in a terminal or shell script.

In the following example, the β€œ$_” parameter will return the last argument of the previous ps command, which is the β€œ-r” flag.

Displaying the last positional parameter of the previous command

Displaying the Specific Argument Based on Position

The β€œ$i” parameter, where β€œi” is referred to as the argument position in β€œ$1” or β€œ$2” assigned to shell script at the time of execution.

In the following example, we used the β€œ$1” parameter, which returns the first argument passed to the shell script.

Displaying the first positional argument assigned to the shell script

In the next example, we used the β€œ$2” parameter, which returns the second argument that was given to the shell script.

Displaying the second positional argument assigned to the shell script

Displaying All of the Shell Script Arguments using $@ Parameter

The β€œ$@” parameter will return all the specified arguments assigned to the referenced shell script, separately.

In the next example, we used the β€œ$@” parameter, which tells the referenced shell script to return all the arguments that were given to it.

Displaying all the positional arguments using the "$@" parameter

Displaying All of the Shell Script Arguments using $* Parameter

The β€œ$*” parameter is identical to the β€œ$@” parameter when specified without quotes, as shown.

Displaying all the positional arguments using the "$*" parameter

Difference Between the $* and $@ Parameters

The nature of this parameter depends on whether it is specified with or without quotes.

Case 1: No quotes around $* and $@ parameters:

In this case,

The [./script.sh arg1 arg2 arg3] with [$*] gets [arg1, arg2, arg3] as separate arguments.

The [./script.sh arg1 arg2 arg3] with [$@] gets [arg1, arg2, arg3] as separate arguments.

In the following script, the β€œ$*” and β€œ$@” parameters are placed without quotes using a β€œfor loop” in bash:

Specifying the "$*" and "$@" without quotes

As you can see without quotes, both parameters treat the user assigned arguments separately.

Case 2: With quotes around the β€œ$*” and β€œ$@” parameters:

The [./script.sh arg1 arg2 arg3] with [β€œ$*β€œ] gets [arg1 arg2 arg3] as a single argument.

The [./script.sh arg1 arg2 arg3] with [β€œ$@β€œ] gets [arg1, arg2, arg3] as separate arguments.

In the following script, the β€œ$*” and β€œ$@” parameters are placed inside quotes using a for loop in bash:

Specifying the "$*" and "$@" with quotes

In this case, when both the parameters are assigned inside quotes, the β€œ$*” treats each assigned argument as a single argument, and the β€œ$@” treats each as a separate argument.

Displaying the Current Flag Set to Your Bash Shell

The β€œ$-” parameter is used to retrieve the current flags assigned to your Bash shell, whether they were set using the set built-in command or by the bash shell itself.

Displaying the flag set to shell

The β€œhimBHs” flag is assigned to my bash shell.

Where:

  • H – histexpand
  • m – monitor
  • h – hashall
  • B – braceexpand
  • i – interactive

Wrap Up

The special parameter is mostly used in shell scripts to perform various tasks like checking the exit status of a recent command or fetching the user assigned arguments.

If any parameters were missed in this article that should have been mentioned, then do let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

2 thoughts on β€œList of Special Parameters in Bash with Examples”

  1. The β€œList of Bash Special Parameters” table at the start of the article has the wrong command for listing the exit status of a command. It shows $! when it should be $?. The explanation for the command later in the article shows the correct command.

    Reply