Linux中的以下环境变量是什么意思?
Oll*_*lli 138
从这里:
$# Stores the number of command-line arguments that
were passed to the shell program.
$? Stores the exit value of the last command that was
executed.
$0 Stores the first word of the entered command (the
name of the shell program).
$* Stores all the arguments that were entered on the
command line ($1 $2 ...).
"$@" Stores all the arguments that were entered
on the command line, individually quoted ("$1" "$2" ...).
Run Code Online (Sandbox Code Playgroud)
所以基本上,$#
是执行脚本时给出的一些参数。$*
是一个包含所有参数的字符串。例如,$1
是第一个参数等等。如果您想访问脚本中的特定参数,这很有用。
正如布赖恩所评论的,这是一个简单的例子。如果您运行以下命令:
./command -yes -no /home/username
Run Code Online (Sandbox Code Playgroud)
$#
= 3$*
= -yes -no /home/username
$@
= 数组: {"-yes", "-no", "/home/username"}
$0
= ./command
, $1
=-yes
等这些是POSIX 标准的一部分,应该得到所有兼容 shell 的支持。作为参考,以下是每个特殊参数的 POSIX 标准定义。请注意,还有三个额外的变量:$-
,$$
和$!
。
$@
:
扩展到位置参数,从 1 开始。当扩展发生在双引号内时,并且在执行字段拆分(参见字段拆分)时,每个位置参数应扩展为一个单独的字段,前提是第一个参数的扩展仍应与开头部分连接原词(假设扩展参数嵌入在一个词中),最后一个参数的扩展仍应与原词的最后一部分连接。如果没有位置参数,“@”的扩展将生成零字段,即使“@”是双引号也是如此。
$*
:
扩展到位置参数,从 1 开始。当扩展发生在双引号字符串中时(请参阅Double-Quotes),它应扩展为单个字段,每个参数的值由 IFS 变量的第一个字符分隔,如果 IFS 未设置,则由 a 分隔。如果 IFS 设置为空字符串,则不等同于取消设置;它的第一个字符不存在,因此连接参数值。
$#
:
扩展到位置参数的十进制数。命令名称(参数 0)不应计入由“#”给出的数字,因为它是一个特殊参数,而不是位置参数。
$?
:
扩展到最近管道的十进制退出状态(请参阅管道)。
$-
:
(连字符。)扩展到当前选项标志(连接成字符串的单字母选项名称),如调用时指定的、由set特殊内置命令指定的或由 shell 隐式指定的。
$$
:
扩展为调用的 shell 的十进制进程 ID。在子shell(请参阅Shell Execution Environment)中,“$”应扩展为与当前shell 相同的值。
$!
:
扩展为从当前 shell 执行的最新后台命令(请参阅列表)的十进制进程 ID 。(例如,从子shell 执行的后台命令不会影响当前shell 环境中“$!”的值。)对于管道,进程ID 是管道中最后一个命令的进程ID。
$0
:
(零。)扩展到 shell 或 shell 脚本的名称。有关如何派生此名称的详细说明,请参阅sh。
归档时间: |
|
查看次数: |
229269 次 |
最近记录: |