What happens when $* is enclosed in double quotes?

093

When $* is enclosed in double quotes ("$*"), it treats all the positional parameters as a single word. This means that all the arguments passed to the script or function will be concatenated into a single string, with the first character of the IFS (Internal Field Separator) variable used as the separator between the arguments.

Example:

Consider a script named example.sh:

#!/bin/bash
echo "Without quotes: $*"
echo "With quotes: \"$*\""

If you run the script with multiple arguments:

./example.sh arg1 arg2 arg3

Output:

  • Without quotes: arg1 arg2 arg3
  • With quotes: "arg1 arg2 arg3"

Key Points:

  • Without Quotes: $* expands to all positional parameters as separate words, preserving spaces between them.
  • With Quotes: "$*" expands to a single string, where all arguments are combined into one word, separated by the first character of IFS (usually a space).

This behavior is useful when you want to treat all arguments as a single entity, especially when passing them to another command or function.

0 Comments

no data
Be the first to share your comment!