'using python variables with bash magic in jupyter

I would like to run bash commands in jupyter notebook using the %%bash magic command and pass python variables. As described in this post I can do this as follows:

%%bash -s {foo} {bar}
cp $1 $2

This works just fine. However, when I have a bunch of these variables and the bash commands are long, it becomes a bit unwieldy to use $1, $2, and so on for arguments. I know that one can use the curly braces notation for line magics as follows:

!cp {foo} {bar}

Is there a comparable way to use the curly brace notation with cell magic? Perhaps something along the lines of:

## in a python cell
foo = 'foo.txt'
bar = 'bar.txt'

## in another cell
%%bash <SOMETHING GOES HERE>
cp {foo} {bar}

UPDATE (04-14-2022): This can be done by defining a new magic as described here.



Solution 1:[1]

Try

!cp $foo $bar

Without the braces.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Wood