Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this script:

ROOT=/tmp
LOG=$ROOT/blah.log
echo $LOG

The output is:

/blah.log

obviously the ROOT variable is not being interpolated which is very unexpected. If I echo $ROOT, it correctly shows /tmp. Hence something fishy is going on here.

share|improve this question
add comment

migrated from serverfault.com Aug 14 '13 at 22:08

This question came from our site for professional system and network administrators.

2 Answers

From the "SIMPLE COMMAND EXPANSION" portion of the bash manpage:

SIMPLE COMMAND EXPANSION When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.

  1. The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.

  2. The words that are not variable assignments or redirections are expanded. If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.

  3. Redirections are performed as described above under REDIRECTION.

  4. The text after the = in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

So, it might sound insane, but the "word expansion" (interpolation) of echo $LOG happens in step 2, and the variables actually get properly expanded in step 4... I'm not sure why you're not simply getting a blank line, but wonder if it was from prior test runs?

Both of these work around that, either by separating into multiple "simple commands", or by interpolating $LOG in a sub-shell:

ROOT=/tmp LOG=$ROOT/blah.log ; echo $LOG

ROOT=/tmp LOG=$ROOT/blah.log sh -c 'echo $LOG'

share|improve this answer
1  
Uh problem solved. I had to delete the extraneous \r characters using sed and now everything works fine. –  user1539343 Aug 12 '13 at 23:01
2  
The question was not properly formatted, so it incorrectly looked like the assignment was happening on the same line as the expansion. –  Dennis Williamson Aug 13 '13 at 11:14
 
@user1539343: I guess this should be closed, then... –  freiheit Aug 13 '13 at 16:29
add comment

Let me guess: you edited that script on Windows.

Correct? Your scripts have Windows line endings: CR LF instead of just LF which is the unix line ending character. As far as the shell is concerned, that CR is part of the command, so the value of ROOT contains a CR at the end, and when you print it out, the CR moves the cursor back to the beginning of the line.

The fix is not to edit scripts on Windows, or else to make sure your editor uses unix line endings. Be sure to convert your existing script(s).

See also Directories are listed twice and Carriage return issue

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.