📜  使用示例演示特殊参数的 Shell 脚本

📅  最后修改于: 2022-05-13 01:57:31.068000             🧑  作者: Mango

使用示例演示特殊参数的 Shell 脚本

在这里,我们将看看 shell 脚本的特殊参数是什么。在这之前,我们先来了解一下什么是shell中的参数。参数是存储值的实体。变量是用户定义的要在该特定 shell 脚本中使用的参数。特殊参数是由 shell 预定义和维护的只读变量。现在让我们看看 bash shell 中的特殊参数是什么。

S.NoSpecial ParametersDescription
1$#This parameter represents the number of arguments passed to the shell script.
2$0This parameter represents the script name.
3 $iThis 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

现在让我们看看演示所有特殊参数的脚本。

#!/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: $-"

现在让我们看看上面脚本的输出: