Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 script.py'
[user@smat-jupyterhub-nb-user ~]$ SCRIPT=script.py

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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
bar

In fact the last one should be rather

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

with ${BAR} additionally quoted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading