Unix 简明教程

Unix / Linux - Special Variables

在本章中,我们将详细讨论 Unix 中的特殊变量。在以前的章节中,我们已经了解了在变量名中使用某些非字母数字字符时的注意事项。这是因为这些字符用于 Unix 特殊变量的名称中。这些变量是为特定函数保留的。

In this chapter, we will discuss in detail about special variable in Unix. In one of our previous chapters, we understood how to be careful when we use certain nonalphanumeric characters in variable names. This is because those characters are used in the names of special Unix variables. These variables are reserved for specific functions.

例如,字符 $ 表示当前 shell 的进程 ID 号或 PID −

For example, the $ character represents the process ID number, or PID, of the current shell −

$echo $$

以上命令会写入当前 shell 的 PID −

The above command writes the PID of the current shell −

29949

下表显示了可以在 shell 脚本中使用的许多特殊变量 −

The following table shows a number of special variables that you can use in your shell scripts −

Sr.No.

Variable & Description

1

$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.

Command-Line Arguments

命令行参数 $1、$2、$3、…​$9 是位置参数,其中 $0 指向实际命令、程序、shell 脚本或函数,$1、$2、$3、…​$9 作为指令的参数。

The command-line arguments $1, $2, $3, …​$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, …​$9 as the arguments to the command.

以下脚本使用与命令行相关的各种特殊变量 −

Following script uses various special variables related to the command line −

#!/bin/sh

echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

下面是上述脚本的一个运行示例——

Here is a sample run for the above script −

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2

Special Parameters $* and $@

有特殊的参数允许一次访问所有命令行参数。 $ * 和 $@ 二者扮演同样的角色,除非它们被包含在双引号中, ""

There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "".

这两个参数都指定了命令行参数。但是,"$*" 特殊参数将整个列表作为一个参数,参数之间有空格,而 "$@" 特殊参数将整个列表分解为单独的参数。

Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into separate arguments.

我们可以编写如下所示的 shell 脚本以使用 $* 或 $@ 特殊参数处理任意数量的命令行参数——

We can write the shell script as shown below to process an unknown number of commandline arguments with either the $* or $@ special parameters −

#!/bin/sh

for TOKEN in $*
do
   echo $TOKEN
done

下面是上述脚本的一个运行示例——

Here is a sample run for the above script −

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

Note ——此处 do…​done 是将涵盖在后续教程中的某种循环。

Note − Here do…​done is a kind of loop that will be covered in a subsequent tutorial.

Exit Status

$? 变量表示上一个命令的退出状态。

The $? variable represents the exit status of the previous command.

退出状态是在命令执行完毕后返回的数值。按照规则,大多数命令如果成功会返回退出状态 0,如果失败会返回 1。

Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

有些命令会出于特定原因返回其他退出状态。例如,有些命令会区分不同类型的错误,并会根据特定类型的错误而返回不同的退出值。

Some commands return additional exit statuses for particular reasons. For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure.

以下是成功命令的示例——

Following is the example of successful command −

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
$echo $?
0
$