Bash – evaluate ENV Variable being stored in a command

I want to save a command in a variable like this:

[user@smat-jupyterhub-nb-user ~]$ TESTCMD='python'
[user@smat-jupyterhub-nb-user ~]$

Running $TESTCMD works fine. But I also want to pass a variable to that command:

[user@smat-jupyterhub-nb-user ~]$ TESTCMD2='python $SCRIPT'

When I run this, I get an error. The Variable does not get evaluated.

[user@smat-jupyterhub-nb-user ~]$ $TESTCMD2
python: can't open file '/jup/projects/$SCRIPT': [Errno 2] No such file or directory

How can I make this variable being evaluated after being stored in a variable itself?

>Solution :

That’s because you used ' quotes, instead of " preventing variable substitution.

$ FOO=echo
$ BAR=bar
$ XXX='${FOO} ${BAR}'
$ $XXX
${FOO}: command not found

$ XXX="${FOO} ${BAR}"
$ $XXX

In fact the last one should be rather

$ XXX="${FOO} "${BAR}""

with ${BAR} additionally quoted.

Leave a Reply