Pass python variables to the magic functions


In [1]:
arg1 = "first_argument"
arg2 = "second_argument"
arg3 = "third_argument"
arg4 = "fourth argument has spaces"
args = [arg1,
        arg2,
        arg3,
        arg4,        
       ]

In [2]:
# Probably the Right Way(tm)
!echo {arg3}


third_argument

In [3]:
!echo $arg4


fourth argument has spaces

In [4]:
# Probably safer than above
!echo "$arg2"


second_argument

In [5]:
# Can do python operations within the curly braces
for i in range(3):
    !echo {i+1}

!echo

!echo "Split the string:" {arg3.split('_')[-1]} {args[2]}

print("")


1
2
3

Split the string: argument third_argument


In [6]:
%%bash -s "$arg1" "$arg2" {arg3}
echo "This bash script knows about $1 and $2 and $3."


This bash script knows about first_argument and second_argument and third_argument.

Grabbing information from ! and magics


In [7]:
year_htmls = !ls 2???.html

In [8]:
year_htmls


Out[8]:
['2005.html',
 '2006.html',
 '2007.html',
 '2008.html',
 '2009.html',
 '2010.html',
 '2011.html',
 '2012.html',
 '2013.html',
 '2014.html',
 '2015.html',
 '2016.html']

In [9]:
type(year_htmls)


Out[9]:
IPython.utils.text.SList

In [10]:
[int(x.split('.')[0]) for x in year_htmls]


Out[10]:
[2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016]

In [ ]: