What Are Parameters in Shell Scripting?


Heather Bennett

In shell scripting, parameters are special variables that hold values passed to a script when it is executed. These values can be provided by users or by other programs or scripts. Parameters play a crucial role in making shell scripts dynamic and versatile, allowing them to work with different inputs and perform a wide range of tasks.

Using Parameters in Shell Scripts

Parameters in shell scripting are represented by numbers and special characters. The most commonly used parameters are:

  • $0 – Represents the name of the script itself
  • $1, $2, .., $n – Represent the positional parameters passed to the script. The number after the dollar sign indicates the position of the parameter.
  • $* – Represents all the positional parameters as a single string
  • $@ – Represents all the positional parameters as separate strings, preserving whitespace and special characters
  • $# – Represents the total number of positional parameters passed to the script
  • $? – Represents the exit status of the last command executed in the script. Typically used to check if a command succeeded or failed.
  • $$ – Represents the process ID (PID) of the current shell script.
  • $! – Represents the PID of the last background command executed in parallel with the current shell script.

Example Usage:

To better understand how parameters work, let’s consider an example. Suppose we have a shell script called “greet.sh” that takes two positional parameters: first name and last name.

The script can be invoked as follows:

$ ./greet.sh John Doe

Within the “greet.sh” script, we can access the first name and last name using $1 and $2, respectively. We can then use these parameters to greet the user:


echo "Hello, $1 $2!"

When executed, the script will output:

Hello, John Doe!

Quoting Parameters:

Quoting parameters is important when dealing with values that contain spaces or special characters. It ensures that the shell treats them as a single argument.

To quote a parameter, you can enclose it in double quotes (“) or single quotes (‘):

$ ./script.sh "Hello World"
$ .sh 'Hello World'

If you fail to quote a parameter containing spaces or special characters, it will be treated as multiple arguments by the shell.


Parameters are a powerful feature in shell scripting that allow scripts to interact with users and other programs. By utilizing parameters effectively, you can create flexible and dynamic scripts capable of handling various inputs and performing complex tasks. Understanding how to use and manipulate parameters is essential for becoming proficient in shell scripting.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy