Bash variable expansion

Hello.
The file /etc/fstab contains

UUID=957c3295-9944-1593-82e2-2b90dede4312 /                     ext4       noatime,discard,acl,user_xattr        1 1

I fill a variable

SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )

echo $SOME_LINE

UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1

echo ${SOME_LINE}

UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1

echo "$SOME_LINE"

UUID=957c3295-9944-1593-82e2-2b90dede4312 /                     ext4       noatime,discard,acl,user_xattr        1 1

I would like to know why The 3 outputs are not the same.

Thank you for helping.

Without quotes, the words in the expansion of $MY_TOKEN are passed to echo as separate arguments by the shell. The echo utility then prints each of its arguments separated by a <space> character and adds a <newline> at the end to produce a line of output.

With quotes, the shell does not perform field splitting and the quoted string is passed to echo as a single argument. And, again, the echo utility prints its argument and adds a <newline> at the end to produce a line of output.

1 Like

MY_TOKEN is not define anywhere, so any output is somewhat surprising...
The first two outputs are identical, and should be - the curly braces are meant to protect a variable name and should not influence the expansion. The shell condenses multiple white spaces into single ones.
Which it doesn't if the to-be-expanded variable is enclosed in double quotes - ALL white space is retained.
Don't mix this up with single quoting which prevents shell expansion and delivers contents a AS IS.

1 Like

Have edited #1 to correct typo.

MY_TOKEN has been replaced by SOME_LINE

Sorry.

---------- Post updated at 23:57 ---------- Previous update was at 23:55 ----------

Thank you everybody.