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

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

  • cat <<< $var does word splitting (followed by joining with space) but not globbing in older versions. For redirections, that depends on whether the posix/sh mode is enabled and the shell is interactive or not. – Stéphane Chazelas May 4 '18 at 13:18
  • Note that "indexed array" is the retronym for ordinary arrays, in contrast to associative array. You are talking about the index to an associative array. – chepner May 4 '18 at 17:43
  • 1
    case $foo in is another. I'm not sure about arguments to parameter expansion; foo="a * b" survives unexpanded in both "${bar:-$foo}" and ${bar:-"$foo"}. I don't recall the details, but I think there is a comment in the source to the effect that the argument itself is not subject to word-splitting or pathname expansion. – chepner May 4 '18 at 17:51
  • I asked the same question on StackOverflow to see if it gets more attention there. – codeforester May 25 '18 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.