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