How to reference both a python and environment variable in jupyter bash magic?

Viewed 220

I would like to add a bash magic cell with both a environment variable and a python variable, like this:

!echo "{1+1}" "$HOME" 

Expecting it to print:

2 /home/ec2-user

However, this does not evaluate the python expression, output is:

{1+1} /home/ec2-user

Removing the environment variable makes the python expression work:

!echo "{1+1}" "HOME" 
2 HOME

What would be the correct syntax for printing both variables?

1 Answers

https://ipython.readthedocs.io/en/stable/interactive/reference.html#system-shell-access

The docs has an example with $$HOME.

In [27]: !echo $HOME
/home/paul
In [28]: !echo $$HOME
/home/paul
In [29]: !echo {i+3} $$HOME
5 /home/paul

Looks like $var can be parsed as either a substitution from a local variable, or pass it on the shell for its evaluation. $$HOME seems to force the shell use. But it's not obvious from the documentation how it handles those details.

Related