LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-12-2009, 11:00 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,544
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Bash: why is word splitting not effective in an assignment


Hello

When bash expands an expression containing a parameter outside double quotes, it first expands the parameter and then tokenises it into IFS-character-separated words as detailed here.

Thus, in
Code:
x='a    df'; echo $x
the "$x" is first expanded to
Code:
'a    df'
and then tokenised to words 'a' and 'df' as evidenced by these examples
Code:
c:~$ x='a    df'
c:~$ echo $x
a df
[in the echo output the 4 spaces have been reduced to one]
c:~$ for i in $x
do 
    echo $i
done
a
df
That being the case y=$x should be equivalent to y=a df but it is not
Code:
c:~$ x='a    df'
c:~$ y=$x
c:~$ y=a    df
Filesystem           1K-blocks      Used Available Use% Mounted on
[snip]
How does that work?!

Best

Charles
 
Old 12-12-2009, 11:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi,

I do believe that one of the first steps taken by the shell when interpreting a command line is splitting it into tokens, expanding expressions is done a few steps later in the process.

This y=a df will be tokenized as:

1) y=a
2) df

The first part doesn't do anything (echo $y -> empty) but doesn't generate an error (which I don't understand ), the second part is interpreted as a command, and executed if legal.

EDIT
As explained by colucix in the next post: The first part does do something. It sets a temporary environment variable.
/EDIT

Last edited by druuna; 12-12-2009 at 11:57 AM. Reason: rectified explanation
 
Old 12-12-2009, 11:46 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by druuna View Post
but doesn't generate an error (which I don't understand
This is the same as running a command and defining an environment variable before the command itself, e.g.
Code:
$ LANG=it_IT date
sab dic 12 18:45:29 CET 2009
In general every variable assignment before a command is valid and correctly interpreted by the shell.
 
1 members found this post helpful.
Old 12-12-2009, 11:49 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
@colucix: Thanks for the explanation!
 
Old 12-12-2009, 11:58 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,544
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Thanks druuna

The GNU Bash Reference in the section on Simple Command Expansion explains the construct y=a df, saying "the variables are added to the environment of the executed command and do not affect the current shell environment". Hence
Code:
c:~$ x=a
c:~$ x=b eval echo '$x'
b
c:~$ echo $x
a
The answer to my question is in the GNU Bash Reference in the section on shell parameters in the text where it describes assignments: "name=[value] If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (detailed below). [snip]. Word splitting is not performed, with the exception of "$@""
 
Old 12-12-2009, 12:02 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,544
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by colucix View Post
This is the same as running a command and defining an environment variable before the command itself, e.g.
Code:
$ LANG=it_IT date
sab dic 12 18:45:29 CET 2009
In general every variable assignment before a command is valid and correctly interpreted by the shell.
Thanks colucix

I didn't see your post while preparing mine. I think it was you who first educated me about this little known shell feature. It allows a horrible interview question: "How can you set an environment variable in bash without using the export keyword?"
 
  


Reply

Tags
assignment, bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [BASH] variable assignment help RaptorX Programming 2 08-30-2009 10:28 AM
trivial: bash, local variable assignment agrestic Linux - Newbie 3 01-27-2009 11:30 AM
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 05:24 PM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
splitting variables In Bash Programming neorion Programming 2 08-04-2004 05:28 PM


All times are GMT -5. The time now is 05:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration