SamuelNLP
SamuelNLP

Reputation: 4136

Export env variable inside double quotes in bash

So this command works fine:

bash -c 'export JUPYTER_HOME=/home/jupyterlab && echo $JUPYTER_HOME'
> /home/jupyterlab

but the double quote variation does not set the variable:

bash -c "export JUPYTER_HOME=/home/jupyterlab && echo $JUPYTER_HOME"
>

I am forced to use double quotes in my problem as this is inside a program I do not control that creates the double-quoted string command. I've tried certain variations like ${JUPYTER_HOME} with no effect.

Upvotes: 0

Views: 1019

Answers (1)

You need to escape the $:

bash -c "export JUPYTER_HOME=/home/jupyterlab && echo \$JUPYTER_HOME"


Explanation

This is because when using double quotes the interpreter is trying to replace $JUPYTER_HOME with its value. For example, check what it happens when you do:

JUPYTER_HOME="what?";bash -c "export JUPYTER_HOME=/home/jupyterlab && echo $JUPYTER_HOME"

It will echo what? because it is the same as executing:

bash -c "export JUPYTER_HOME=/home/jupyterlab && echo what?"

In your case, where $JUPYTER_HOME had no value it was actually executing:

bash -c "export JUPYTER_HOME=/home/jupyterlab && echo "

Upvotes: 3

Related Questions