"$@" 在 bash 脚本中有什么作用?

DK *_*ose 6 bash scripts

我遇到了这个脚本

#!/bin/sh
qemu-system-x86_64 -enable-kvm \
                   -m 2G \
                   -device virtio-vga,virgl=on \
                   -drive file=/home/empty/qemubl/lab.img,format=raw,if=virtio \
                   -cpu host \
                   -smp 4 \
                   -soundhw sb16,es1370 \
                   "$@"
Run Code Online (Sandbox Code Playgroud)

的作用是$@什么?

我搜索man bash$@,但得到Pattern not found

des*_*ert 11

您的搜索可能没有结果,因为(如steeldriver 评论)您没有逃脱$,尝试搜索\$@,您会在man bash– 参数 – 特殊参数下找到以下内容:

@  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 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).
Run Code Online (Sandbox Code Playgroud)

$1 $2 $3 … ${N}引用它会扩展为,引用为"$1" "$2" "$3" … "${N}",这在大多数情况下是您想要的。bash-hackers wiki 中$*很好地解释了这一点和 to 的区别。


dob*_*bey 5

它是一个内部变量,用于将传递给脚本的所有参数转发给脚本正在调用的命令,在本例中为qemu-system-x86_64