Do here-strings undergo word-splitting?
Asked Answered
C

1

16

Quoting Bash Reference Manual and man bash (version 4.3):

[n]<<< word

The word undergoes brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion and word splitting are not performed.

The word should not undergo word-splitting. However, using this simple code:

var="a    b"

cat <<< $var
#output:
#a b

cat <<< "$var"
#output:
#a    b

What am I missing? Does this depend on the version of bash or is there a mistake in the manual? I am using GNU bash, version 4.3.48.

Capitoline answered 11/12, 2017 at 0:8 Comment(8)
Unlikely a version thing: I get the same with GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)Hierarchy
On my bash 4.4.12(1), both versions of the cat command produce the same output (with no word-splitting).Serena
@Serena I guess it is corrected in bash 4.4. The part about no word-splitting is still present in my bash 4.3 manual from 2014 February though.Capitoline
@Capitoline To clarify, my man bash (version 4.4) says no word splitting is performed and a repeat of your tests on my machine shows no word splitting.Serena
Fwiw, man bash on OSX Sierra (the above mentioned version of bash) says nothing about word splitting for here strings.Hierarchy
@TylerH This question asks about here-strings in particular, not about strings in general. I had to rollback your edit.Capitoline
@Capitoline Thanks, the issue was that without the hyphen it looked like a random word incorrectly inserted into the question.Delk
@Delk Yep, looks better now, thanks :)Capitoline
P
12

The change from splitting here-strings to not splitting them happened between bash-4.4-alpha and bash-4.4-beta. Quoting from CHANGES:

Bash no longer splits the expansion of here-strings, as the documentation has always said.

So the manuals of older Bash versions mentioned it, but didn't actually do it.

Porphyry answered 11/12, 2017 at 1:6 Comment(1)
Nice find! I know where to look for things like this at last. I checked that versions prior to 4.3 didn't mention word-splitting at all but since 4.3 it has been present in manuals. For sure took them a while to comply with it.Capitoline

© 2022 - 2024 — McMap. All rights reserved.