Open In App

Shell Script to Demonstrate Special Parameters With Example

Improve
Improve
Like Article
Like
Save
Share
Report

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.

S.No Special Parameters Description
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 Variables

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 Variable

Exit Special Parameters

Conclusion

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.



Last Updated : 03 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads