使用两个变量($@,$*)将命令的参数传输到脚本中的目的是什么?

使用两个变量($@,$*)将命令的参数传输到脚本中的目的是什么?

我已经阅读了脚本$@和中使用的特殊变量$*。据我了解,执行脚本时使用的参数存储在两个特殊变量中,一旦所有参数进入$@,一旦所有参数进入$*,这样就可以在脚本内访问它们。

我不明白为什么同一组参数必须有两个特殊变量。什么时候使用一个特殊变量和另一个特殊变量有什么区别?

在此输入图像描述

答案1

简单原始的解释是:

  • $*所有设置的参数都是一个字符串(参数由 中的第一个字符分隔$IFS
  • $@每个参数都是不同的字符串(参数由换行符分隔)

man bash

* Expands to the positional parameters, starting from one.  When the expansion is not within  dou‐
  ble  quotes, each positional parameter expands to a separate word.  In contexts where it is per‐
  formed, those words are subject to further word splitting  and  pathname  expansion.   When  the
  expansion occurs within double quotes, it expands to a single word with the value of each param‐
  eter separated by the first character of the IFS special variable.  That is, "$*" is  equivalent
  to  "$1c$2c...",  where  c  is  the first character of the value of the IFS variable.  If IFS is
  unset, the parameters are separated by spaces.  If IFS is null, the parameters are joined  with‐
  out intervening separators.
@ Expands  to the positional parameters, starting from one.  When the expansion occurs within dou‐
  ble quotes, each parameter expands to a separate word.  That is, "$@" is equivalent to "$1" "$2"
  ...   If  the double-quoted expansion occurs within a word, the expansion of the first parameter
  is joined with the beginning part of the original word, and the expansion of the last  parameter
  is  joined  with  the  last part of the original word.  When there are no positional parameters,
  "$@" and $@ expand to nothing (i.e., they are removed).

答案2

除此之外:

  • "$*"扩展到"arg1 arg2 arg3 …"
  • "$@"扩展到"arg1" "arg2" "arg3" …

从而"$@"更安全。$*可能较旧,存在是为了向后兼容。

答案3

man bash

Special Parameters
   The  shell treats several parameters specially.  These parameters may only
   be referenced; assignment to them is not allowed.
   *      Expands to the positional parameters, starting from one.  When  the
          expansion  is  not  within double quotes, each positional parameter
          expands to a separate word.  In contexts  where  it  is  performed,
          those  words  are  subject  to  further word splitting and pathname
          expansion.  When the expansion  occurs  within  double  quotes,  it
          expands to a single word with the value of each parameter separated
          by the first character of the IFS special variable.  That is,  "$*"
          is equivalent to "$1c$2c...", where c is the first character of the
          value of the IFS variable.  If IFS is  unset,  the  parameters  are
          separated  by  spaces.   If  IFS is null, the parameters are joined
          without intervening separators.
   @      Expands to the positional parameters, starting from one.  When  the
          expansion  occurs within double quotes, each parameter expands to a
          separate word.  That is, "$@" is equivalent to "$1"  "$2"  ...   If
          the  double-quoted expansion occurs within a word, the expansion of
          the first parameter is joined with the beginning part of the origi‐
          nal  word,  and  the expansion of the last parameter is joined with
          the last part of the original word.  When there are  no  positional
          parameters, "$@" and $@ expand to nothing (i.e., they are removed).

比较这四种情况,尤其是包含空格的参数。

for i in $*; do echo "$i"; done
for i in $@; do echo "$i"; done
for i in "$*"; do echo "$i"; done
for i in "$@"; do echo "$i"; done

答案4

特殊参数$*和$@:

有一些特殊参数允许一次访问所有命令行参数。 $* 和 $@ 的作用相同,除非它们用双引号 "" 括起来。

这两个参数都指定所有命令行参数,但“$*”特殊参数将整个列表作为一个参数,中间有空格,而“$@”特殊参数则将整个列表分为单独的参数。

我们可以使用 $* 或 $@ 特殊参数编写 shell 脚本来处理未知数量的命令行参数:

相关内容