3

Bash doesn't perform word splitting in globbing in these cases:

LHS or RHS of an assignment, except for indexed arrays

var=$value                                # simple variable
declare -A hash
key="key with a space"
hash[$key]=$value                         # index of an associative array

arr=$(echo "1 2 3")                       # word splitting does happen here

Inside [[ ]]

var="one two"
if [[ $var = *" "* ]]; then ...           # check if var has a space in it
if [[ $(echo "one two") = $var ]]; then   # use the output of command substitution to compare with var

Inside (( ))

((sum = $(echo "99 + 1")))                  # assigns 100 to sum

In herestring

cat <<< *                                 # gives '*' as the output

In a case statement

var="hello world"
case $var in
  "hello world") echo "matches!";;
  ...
esac

Is there a definite list of cases where Bash does or doesn't perform word splitting and globbing?

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 5
    There's a whole section in the Bash manual on [Shell Expansions](3.5 Shell Expansions) which goes through the details of the sequence in which expansions occur, and the various sections describing the various constructs you list — and the ones you don't...`for`, `$((…))`, etc. – Jonathan Leffler May 25 '18 at 23:46
  • 2
    Cross-posted on [U&L StackExchange](https://unix.stackexchange.com/questions/441690/what-are-the-contexts-where-bash-doesnt-perform-word-splitting-and-globbing), and has an exhaustive answer there. – AdminBee Jul 22 '20 at 09:03

0 Answers0