echo an environment variable in bash

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?

1 Answer

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

Cf.

text=blah ; echo $text

or

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like