Passing IPython variables as arguments to bash commands

PythonBashJupyter NotebookIpythonCommand Line-Arguments

Python Problem Overview


How do I execute a bash command from Ipython/Jupyter notebook passing the value of a python variable as an argument like in this example:

py_var="foo"
!grep py_var bar.txt

(obviously I want to grep for foo and not the literal string py_var)

Python Solutions


Solution 1 - Python

Prefix your variable names with a $.

Example

Say you want to copy a file file1 to a path stored in a python variable named dir_pth:

dir_path = "/home/foo/bar"
!cp file1 $dir_path

from Ipython or Jupyter notebook

EDIT

Thanks to the suggestion from Catbuilts, if you want to concatenate multiple strings to form the path, use {..} instead of $..$. A general solution that works in both situations is to stick with {..}

dir_path = "/home/foo/bar"
!cp file1 {dir_path}

And if you want to concatinate another string sub_dir to your path, then:

!cp file1 {dir_path + sub_dir}

EDIT 2

For a related discussion on the use of raw strings (prefixed with r) to pass the variables, see https://stackoverflow.com/q/61606054/937153

Solution 2 - Python

You cans use this syntax too:

path = "../_data/"
filename = "titanicdata.htm"
! less {path + filename}

Solution 3 - Python

As @Catbuilts points out, $'s are problematic. To make it more explicit and not bury the key example, try the following:

afile='afile.txt'
!echo afile
!echo $PWD
!echo $PWD/{afile}
!echo {pwd+'/'+afile}

And you get:

afile.txt
/Users/user/Documents/adir
/Users/user/Documents/adir/{afile}
/Users/user/Documents/adir/afile.txt

Solution 4 - Python

Just an addition. In my case, and as shown in some of the examples in this question, my arguments were file names with spaces. Is that case I had to use a slightly different syntax: "$VAR". An example would be

touch "file with spaces.txt"
echo "this is a line" > "file with spaces.txt"
echo "this is another line" >> "file with spaces.txt"
echo "last but not least" >> "file with spaces.txt"
echo "the last line" >> "file with spaces.txt"
cat "file with spaces.txt"

# The variable with spaces such as a file or a path
ARGUMENT="file with spaces.txt"
echo $ARGUMENT

# The following might not work
cat $pwd$ARGUMENT

# But this should work
cat $pwd"$ARGUMENT"

I hope this helps. ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnniView Question on Stackoverflow
Solution 1 - PythonUnniView Answer on Stackoverflow
Solution 2 - PythonsloikView Answer on Stackoverflow
Solution 3 - PythonpaulperryView Answer on Stackoverflow
Solution 4 - PythoneliasmaxilView Answer on Stackoverflow