Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Bash variable expansion

Shell Programming and Scripting


Tags
shell quoting, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-18-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
Bash variable expansion

Hello.
The file /etc/fstab contains

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

I fill a variable

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

echo $SOME_LINE

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

echo ${SOME_LINE}

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

echo "$SOME_LINE"

Code:
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.

Last edited by jcdole; 05-18-2017 at 05:51 PM..
Sponsored Links
    #2  
Old 05-18-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 18 October 2017, 5:21 AM EDT
Location: San Jose, CA, USA
Posts: 10,536
Thanks: 548
Thanked 3,689 Times in 3,147 Posts
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
jcdole (05-18-2017)
Sponsored Links
    #3  
Old 05-18-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 October 2017, 8:58 AM EDT
Location: Aachen, Germany
Posts: 11,473
Thanks: 309
Thanked 3,556 Times in 3,273 Posts
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.
The Following User Says Thank You to RudiC For This Useful Post:
jcdole (05-18-2017)
    #4  
Old 05-18-2017
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash expansion jcdole Shell Programming and Scripting 8 02-14-2017 07:02 AM
Bash shell expansion help cokedude Shell Programming and Scripting 5 09-19-2015 05:12 AM
Bash variable delayed expansion? premotheus Shell Programming and Scripting 4 07-09-2010 05:07 AM
Brace expansion problem in Bash mglenney Shell Programming and Scripting 5 07-03-2009 03:07 AM
bash - delay expansion of variable scandora Shell Programming and Scripting 3 02-18-2009 04:59 AM



All times are GMT -4. The time now is 09:07 AM.