vkkodali
vkkodali

Reputation: 680

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.

Upvotes: 8

Views: 5507

Answers (1)

Stephen Wood
Stephen Wood

Reputation: 1345

Try

!cp $foo $bar

Without the braces.

Upvotes: 7

Related Questions