Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to know how to set up an environment variable that stores other environment variable but these environment variables doesn't gets expanded when I echo instead if I use eval echo then it should expand.

Like i want to have echo $TEST_HOME should result in /Users/krysto/$PATH/$DATA_DIR

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Create TEST_HOME using single-quotes to prevent bash expansion:

$ TEST_HOME='/Users/krysto/$MYPATH/$DATA_DIR'
$ echo $TEST_HOME
/Users/krysto/$MYPATH/$DATA_DIR

Then, one can use eval to expand it.  As an example:

$ MYPATH=dir1/dir2
$ DATA_DIR=data/dir
$ eval echo $TEST_HOME
/Users/krysto/dir1/dir2/data/dir

A couple of notes: The convention is that the important system variables, like PATH or HOME, are all upper-case. If you want to avoid overwriting one of them, make your variables lower-case. In particular, overriding PATH could leave the shell that you are in largely non-functional.

share|improve this answer

Just escape the variables (makes no diffirence if local or env), e.g.

> test="echo something"
> test1="\$test"
> echo $test1
$test
> eval $test1
something
share|improve this answer

Your Answer

 
discard

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

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