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 Code:
'a df' Code:
c:~$ x='a df' Code:
c:~$ x='a df' Best Charles |
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 |
Quote:
Code:
$ LANG=it_IT date |
@colucix: Thanks for the explanation!
|
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 |
Quote:
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?" |
All times are GMT -5. The time now is 05:56 AM. |