Shell Script to Demonstrate Special Parameters With Example
Last Updated :
03 Jan, 2024
Here, we are going to see what are the special Parameters of the shell script. Before that first, let's understand what is parameters in the shell. The parameter is the entity that stores the value. The variables are the parameters that are defined by the user to use in that specific shell script. And the Special parameters are the read-only variables that are predefined and maintained by the shell. Now let's see what are the Special parameters in the bash shell.
|
1 | $# | This parameter represents the number of arguments passed to the shell script. |
2 | $0 | This parameter represents the script name. |
3 | Â $i | This parameter represents the ith argument passed to the shell script like $1,$2 |
4 | $* | This parameter gives all arguments passed to the shell script separated by the space. |
5 | $! | This parameter gives PID of the last background running process. |
6 | $? | This parameter represents the exit status of the last command that executed. The 0 code represents success and 1 represents failure. |
7 | $_ | This parameter gives the last argument provided to the previous command that executed. |
8 | $$ | This parameter gives the PID of the current shell. |
9 | $@ | This parameter holds all argument passed to the script and treat them as an array. It is similar to the $* parameter |
10 | Â $-Â | This parameter represents the current flags set in your shell .himBH are the flags in bash shell.
Where:
- H - histexpand
- m - monitor
- h - hashall
- B - braceexpand
- i - interactive
|
Now let's see the script which demonstrates all Special Parameters.
#!/bin/bash
echo "Number of argument passed: $#"
echo "Script name is $0"
echo "The 2nd argument passed is: $2"
echo "Arguments passed to script are: $*"
echo "Exit status of last command that executed:$?" #This is the previous command for $_
echo "Last argument provide to previous command:$_"
echo "PID of current shell is: $$"
echo "Flags are set in the shell: $-"
Explanation:
#!/bin/bash
: This is known as a shebang line, and it indicates that the script should be interpreted using the Bash shell.echo "Number of argument passed: $#
"
: This line prints the number of arguments passed to the script using the special parameter $#
.echo "Script name is $0"
: This line prints the name of the script itself using the special parameter $0
.echo "The 2nd argument passed is: $2"
: This line prints the second argument passed to the script using the special parameter $2
.echo "Arguments passed to script are: $*"
: This line prints all the arguments passed to the script using the special parameter $*
.echo "Exit status of the last command that executed: $?"
: This line prints the exit status of the last command that was executed using the special parameter $?
. The exit status is a numeric value that indicates whether the last command was successful (0) or encountered an error.echo "Last argument provided to the previous command: $_"
: This line prints the last argument to the previous command using the special parameter $_
.echo "PID of the current shell is: $$"
: This line prints the process ID (PID) of the current shell using the special parameter $$
.echo "Flags are set in the shell: $-"
: This line prints the flags that are currently set in the shell using the special parameter $-
. The flags represent various settings and options in the shell environment.
Now let's see the output of the above script:

Special Parameters in Shell Scripting: $* and $@
In the realm of shell scripting, there exist special parameters that facilitate the retrieval of all command-line arguments collectively. The parameters $* and $@ behave similarly unless enclosed in double quotes, "".
Both $* and $@ serve to denote command-line arguments. However, the "$*" special parameter treats the entire list as a singular argument with spaces in between, whereas the "$@" special parameter preserves the individuality of each argument in the list.
Consider the following example of a shell script designed to handle an unspecified number of command-line arguments using either the $* or $@ special parameters:
#!/bin/bash
for TOKEN in $*
do
echo $TOKEN
done
Here's a sample execution of the above script:
Special Parameters
Exit Status in Shell Scripting
The variable $? serves as a representation of the exit status of the preceding command.
Exit status is a numerical value returned by each command upon completion. Typically, a successful command returns an exit status of 0, while an unsuccessful one returns 1. Some commands may yield different exit statuses to convey specific types of failures.
Here's an example of a successful command:
#!/bin/bash
echo "File Name: $0"
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Quoted Values: \"$1 $2\""
echo "Quoted Values: \"$*\""
echo "Total Number of Parameters: $#"
Save this script as `example.sh
`and make it executable using the command:
chmod +x example.sh
Now, when you run the script with the provided arguments:
./example.sh jhon Jack
Finally, if you check the exit status using `echo $?
`, it should display `0`
:
echo $?
Exit Special ParametersConclusion
In this article we discussed the significance of special parameters in shell scripting, explaining their role as predefined and read-only variables maintained by the shell. Key parameters like $#, $0, $, and others are explored, showcasing their utility in tasks such as handling command-line arguments and reporting exit statuses. A practical script illustrates the application of these parameters, providing a comprehensive understanding of their functionality. The distinction between $ and $@ is highlighted, and the script demonstrates their use in capturing and processing command-line arguments. Overall, the article provides a concise overview of essential shell script parameters and their practical implementation.
Similar Reads
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
Introduction to Shell Scripting
Basic Shell Commands in Linux
Basic Shell Commands in Linux: Complete List
Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively.The Linux shell serves as an interface for us
5 min read
Linux Directory Structure
Prerequisite: Linux File Hierarchy Structure In Linux/Unix operating system everything is a file even directories are files, files are files, and devices like mouse, keyboard, printer, etc are also files. Here we are going to see the Directory Structure in Linux. Types of files in the Linux system.
5 min read
Input Output Redirection in Linux
In Linux, whenever an individual runs a command, it can take input, give output, or do both. Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so. For example, if we run the "date" command, it giv
4 min read
Functions and Script Organization
Command-Line Arguments and Options
Error Handling and Debugging