[IPython-dev] Pass variable to a ! command

Matthias Bussonnier bussonniermatthias at gmail.com
Wed Sep 24 03:01:58 EDT 2014


Le 24 sept. 2014 à 05:39, Kenneth Tran <one at kentran.net> a écrit :

> Hi,
> 
> I'd like to pass Python variables as arguments to a system command. However, perhaps due to the command line syntax of my program (which accepts nested options via {}), this doesn't work.
> 
> <image.png>
> 
> It looks like that when IPython fails to parse one of the variable names or expressions, it treats the whole command line as raw string. Here's a simpler example
> 
> <image.png>
> 
> Should it try to parse variable names and/or expressions as much as it can? 


No it does bail out on first error. 

escape brackets :

In [6]: bar = 1

In [7]: ! echo \{bar\}
{bar}

or use an intermediate string. 

In [8]: foo = '{bar}'

In [9]: !echo {foo}
{bar}

to pass the dollars raw, use double dollar. $$

See explanation here:

https://github.com/ipython/ipython/issues/6527

> What files should I look into if I want to fix this?

I'm not sure it is something that can be "fixed" (i.e., the current behavior have probably have reasons).

But it is the %sx magic IPython/core/magics/osm.py
And might be a general behavior of line magic to expand $foo and {foo}.
-- 
M


> 
> Thanks.
> -Ken 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140924/c1362ecd/attachment.html>


More information about the IPython-dev mailing list