“$@” 在 bash 脚本中起什么作用?

“$@” 在 bash 脚本中起什么作用?

我碰到这个脚本

#!/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 \
                   "$@"

的作用是什么$@

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

答案1

您的搜索可能没有结果,因为(如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).

不加引号时,它会扩展为$1 $2 $3 … ${N},加引号时会扩展为"$1" "$2" "$3" … "${N}",在大多数情况下,这正是您想要的。这和 的区别$*bash-hackers 维基

答案2

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

相关内容