I have the following variables in my .bashrc:

export UPRODUCT=productName
export U=/path/to/products/$UPRODUCT

So typing cd $U will take me to /path/to/products/productName.

What I want to do is, in my shell

export UPRODUCT=otherProductName
cd $U

and be taken to /path/to/products/otherProductName, without re-defining $U. Is this possible?


Direct answer: No, it is not possible to save an unexpanded reference in a variable that will be expanded when the variable is used. Shell expansions happen immediately after tokenization, including for variable assignment, and parameter expansion does not include a recursive expansion step itself.

edit, per Kevin: Note, however, that other expansions may occur AFTER shell parameter expansion (including filename expansion). This doesn't allow for double indirection, but may be used to expand unquoted filename wildcards.

Use one of the other answers on the page for equivalent behavior.

  • "you can't save "*.txt" into a variable to be expanded in the environment where it's used" -- In sane shells (zsh), yes. In bash, that's exactly what happens. Try it yourself. [might be a settable option, but definitely the default] – Kevin Jul 9 '15 at 4:17
  • @Kevin See: pastebin.com/5JiztXhh – Jeff Bowman Jul 9 '15 at 5:24
  • @JeffBowman take $files2 out of the quotes. – Kevin Jul 9 '15 at 5:28
  • @Kevin Fair, I stand corrected. I stand by my original statement, but have edited for clarification. Thank you! – Jeff Bowman Jul 9 '15 at 5:29

Yes with a function

cdu() { cd /path/to/products/$UPRODUCT ;}

you type cdu and it will change in the directory $U. I call it cdu but you can choose the name you prefer.
The question is that when you export the 1st time in the .bashrc it expands the value known at that moment.

  • This (and others) are all workable answers, but it doesn't specifically answer my question of whether it's possible to make the export in .bashrc not expand the $UPRODUCT variable until I try to access the $U varaible. – Nick Jul 8 '15 at 18:57
  • Whatever you prefer, really... but I read a different question: What I want to do is, in my shell export UPRODUCT=otherProductName and after cd $U_and be taken to /path/to/products/otherProductName, without re-defining $U. Is this possible?. Yes with an alias or a function you can change directory changing only the variable $UPRODUCT, and you do not need to re-define $U. – Hastur Jul 8 '15 at 20:57

Add this to your ~/.bashrc:

alias mycd='export U=/path/to/products/$UPRODUCT; cd'

and use it this way:

export UPRODUCT=otherProductName
mycd $U

You could use a small script for this to get it all done with one command.

Something like this:

 ## script must take one argument
 ## the argument will be the otherProductName

 if test "$1" != ''
   echo changing $UPRODUCT to $1 #Using the argument for new name
   export UPRODUCT=$1 #exporting that variable
   ## CHANGE /path/to/products/ to your actual path!!!
   export U=/path/to/products/$UPRODUCT #changing U to new path
   echo new UPRODUCT is: $UPRODUCT #displaying new variables
   echo new U is: $U
   if [ ! -d $U ]  #if new product doesn't exist as directory
     echo $U not found
     cd $U  # cd to new directory
  echo 'USAGE: change_path.sh [newProductName]' # prompt for new name

Make a new file called change_path.sh (or whatever you want) and give it executable permissions.

chmod 755 change_path.sh

Then when you want to change UPRODUCT just execute that file

./change_path.sh [otherProductName]


If you are willing to use eval (and I hope you're not), then you can do this:

export UPRODUCT=productName
export U=/path/to/products/\$UPRODUCT

export UPRODUCT=otherProductName
eval cd $U

Note the \ before the $ in the definition of the U variable.

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.