Unix/Linux 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 Unix and Linux 1 Week Ago
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 19 May 2017, 3:58 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 160
Thanks: 54
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; 1 Week Ago at 05:51 PM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 29 May 2017, 4:14 AM EDT
Location: San Jose, CA, USA
Posts: 10,309
Thanks: 521
Thanked 3,587 Times in 3,055 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2017, 6:37 AM EDT
Location: Aachen, Germany
Posts: 10,830
Thanks: 272
Thanked 3,324 Times in 3,061 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 (1 Week Ago)
    #4  
Old Unix and Linux 1 Week Ago
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 19 May 2017, 3:58 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 160
Thanks: 54
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

Linux 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 07:32 AM.