Bash variable inline newline at the end of the variable

I read Attempting to embed a newline in a variable in bash , and I think I understand the newline as IFS, and how bash changes newline characters in spaces at times, but I don't understand this situation:

[prompt]$ blah="$(printf "hi\n\n\n\n")"
[prompt]$ echo "$blah"
hi
[prompt]$ blah="$(printf "hi\n\n\n\nx")"
[prompt]$ echo "$blah"
hi



x

Why didn't the first echo spit out a bunch of new lines? Thanks.

+4
source share
1 answer

Because what the spec says the shell should do. Namely, separate the sequence of lines of a new line.

From the specification (my selection):

, (. Shell Execution Environment) ( "$()" backquotes) . ; IFS , . , .

+5

All Articles