What does "$@" do in a bash script? [duplicate]
I came across this script:
#!/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 \
"$@"
What is the role of $@
?
I searched man bash
for $@
but got Pattern not found
.
Solution 1:
Your search may have yielded no result because (as steeldriver commented) you didn't escape the $
, try searching for \$@
and you'll find the following under man bash
– PARAMETERS – Special Parameters:
@ 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).
Unquoted it gets expanded to $1 $2 $3 … ${N}
, quoted to "$1" "$2" "$3" … "${N}"
, which is what you want in most cases. This and the difference to $*
is very good explained in the bash-hackers wiki.
Solution 2:
It's an internal variable which is used to forward all arguments passed to the script, to the command which the script is calling, in this case qemu-system-x86_64
.