I don't understand why this doesn't work:

TEXT="blah" echo $TEXT    #echoes nothing

I thought it might be because echo is a builtin, so I tried this:

TEXT="blah" `which echo` $TEXT    #still nothing

What am I missing?


The shell expands the variables before it runs the command, even before it runs the assignments.


text=blah ; echo $text


text=blah eval 'echo $text'

The first one works because the assignment is run as a separate command. The second one works because $text is single-quoted which prevents its expansion when shell is processing the command; when eval is later running, the variable already has the value assigned.

  • OK, I see. So if TEXT had been "foo" beforehand, TEXT="blah" echo $TEXT would have been expanded to TEXT="blah" echo foo before being executed.
    – Simonski
    Oct 31 '18 at 13:23
  • Exactly, and it's very easy to try: text=blah; text=new echo $text.
    – choroba
    Oct 31 '18 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.