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:
Shell Script to Demonstrate Special Parameters With Example
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.
Table of Content
- Now let’s see the script which demonstrates all Special Parameters.
- Special Parameters in Shell Scripting: $* and $@
- Exit Status in Shell Scripting
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:
|
Contact Us