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.

PesaThe
  • 7,259
  • 1
  • 19
  • 43
  • 1
    Unlikely a version thing: I get the same with ```GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)``` – MASL Dec 11 '17 at 00:11
  • On my bash 4.4.12(1), both versions of the `cat` command produce the same output (with no word-splitting). – John1024 Dec 11 '17 at 00:15
  • 1
    @John1024 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. – PesaThe Dec 11 '17 at 00:23
  • 1
    @PesaThe 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. – John1024 Dec 11 '17 at 00:29
  • 1
    Fwiw, ```man bash``` on OSX Sierra (the above mentioned version of bash) says nothing about word splitting for here strings. – MASL Dec 11 '17 at 00:35
  • @TylerH This question asks about here-strings in particular, not about strings in general. I had to rollback your edit. – PesaThe Jan 05 '18 at 17:47
  • @PesaThe Thanks, the issue was that without the hyphen it looked like a random word incorrectly inserted into the question. – TylerH Jan 05 '18 at 18:57
  • @TylerH Yep, looks better now, thanks :) – PesaThe Jan 05 '18 at 19:06

1 Answers1

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 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. – PesaThe Dec 11 '17 at 11:02