What happens when $@ is enclosed in double quotes?

057

When $@ is enclosed in double quotes in a shell script, it treats each argument as a separate quoted string. This means that if there are multiple arguments passed to the script, each argument will be preserved as a distinct entity, even if they contain spaces.

For example, consider the following script:

#!/bin/bash
echo "All arguments: $@"
echo "All arguments (quoted): \"$@\""

If you run this script with the command:

./script.sh "arg1" "arg 2" "arg3"

The output will be:

All arguments: arg1 arg 2 arg3
All arguments (quoted): "arg1" "arg 2" "arg3"

In this case, the second argument ("arg 2") retains its space because it is treated as a single argument when enclosed in quotes.

0 Comments

no data
Be the first to share your comment!