shell 脚本里面的特殊变量

tech2026-08-08  1

Sr.No.Variable & Description1

$0

The filename of the current script.

2

$n

These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).

3

$#

The number of arguments supplied to a script.

4

$*

All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. 获取所有的命令行参数

5

$@

All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.获取所有的命令行参数

6

$?

The exit status of the last command executed.

7

$$

The process number of the current shell. For shell scripts, this is the process ID under which they are executing.

8

$!

The process number of the last background command.

举个小例子:

#!/bin/sh for TOKEN in $* do echo $TOKEN done

现实结果如下面:

$./test.sh Zara Ali 10 Years Old Zara Ali 10 Years Old

 

 

最新回复(0)