Pass every script argument to another command without splitting arguments that contain spaces.
Which expansion is appropriate?
"$@" expands each positional parameter as a separate quoted argument.
Detailed explanation
"$@"Correct. "$@" preserves each original argument as a separate argument.
Correct. "$@" preserves each original argument as a separate argument.
$@Incorrect. Unquoted expansion can undergo word splitting and pathname expansion.
Incorrect. Unquoted expansion can undergo word splitting and pathname expansion.
"$*"Incorrect. "$*" joins all positional parameters into one string.
Incorrect. "$*" joins all positional parameters into one string.
$*Incorrect. Unquoted $* does not preserve the original argument boundaries.
Incorrect. Unquoted $* does not preserve the original argument boundaries.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'printf "<%s>\n" "$@"' demo 'two words' tailExpected result
<two words>と<tail>の2行Key points
- $@ means all positional arguments
- Double quotes preserve boundaries
- Difference from $*
Notes
- Environment: GNU Bash 5.2 / 短命シェル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.