Posts Tagged ‘bash’

Insert the path of the Python interpreter easily

December 15, 2013 Leave a comment

How many times have you written “#!/usr/bin/env python” in your life? A few hundred times? :) How to insert this line easily?

Solution #1
I mainly use vim. So far I have done it the following way.

In vim, type this on the first line in normal mode. This is the mode that vim will usually start in, which you can usually get back to with ESC. At the end press ENTER.

!!which env<ENTER>

!!” brings you to command mode and the current line (which was empty) is replaced with the output of “which env“. Thus the first line becomes “/usr/bin/env“. All you need to do is add “#!” and “python” manually.

Fine, but I got fed up with this. It’s still too much typing. How to do it easier?

Solution #2
Write a bash script called “py” with the following content:

echo "#!`which env` python"

Put it somewhere in your PATH and make it executable.

Then, in vim type this on the first line:


Phew. I should have thought of it years ago.

Categories: python Tags: , ,

Python from command line

September 18, 2013 Leave a comment

You want to calculate something with Python quickly, from the command line. You might even want to use Python in a bash script to produce some result.


$ python -c "print 2*3"

Storing the result in a variable:

$ X=`python -c "print 2*3"`
$ echo $X

Thanks to Tajti A. for the tip.

Update (20170803)
You can also pass a bash variable to the embedded Python:

VAL="cat dog cat"                                                                                                                                                                             
NEW=`python3 -c "print('$VAL'.replace('dog', 'wolf'))"`                                                                                                                                       
echo $NEW

Output: “cat wolf cat”.

Categories: python Tags: , , ,

bash-like functionalities in command-line Python script

You have an interactive Python script that reads input from the command line. You want to add bash-like functionalities to it like moving the cursor with the arrows, jump to the front with Home, jump to the end with End, browse previous commands with the up arrow, etc.

You won’t believe what is needed for this:

import readline

Yes, that’s it. Just import it and you are good to go.

Minimal example:

import readline

while True:
    inp = raw_input("> ")
    print "You entered", inp

Before discovering the readline module, I used to start my scripts with “rlwrap“:


It does the trick too. I put the line above in a script called “” and I launched this latter one. However, “import readline” is simpler.

Categories: python Tags: , , ,

rlwrap: adding keyboard editing functionalities to your scripts

January 1, 2013 Leave a comment

You have an interactive script that waits for some user input, similarly to the (Python) shell. In bash, you can use the arrow keys for editing / browsing, but it’s not available in a Python script right away. If you read the user input with raw_input() for instance, you cannot move the cursor back with the left arrow, or you cannot browse previous commands with the up arrow.

And still… How to add such bash-like functionalities to your script? The method must be absolutely painless, of course.

Use the command “rlwrap“, which magically adds these functionalities to any script. Awesome. So instead of launching your script with “./“, launch it like this:

$ rlwrap

You might have to install rlwrap with “sudo apt-get install rlwrap“. More info about rlwrap: here.

Categories: python Tags: , , ,

Write Python inside Bash

November 24, 2012 2 comments

You want to embed Python code inside a Bash script.

Here is a possible solution:



# Example:
# Write a bash script that has one parameter: your name.
# It prints every second character of your name.

TFILE="`basename $0`.$$.py"

cat <<END >$TFILE
#!/usr/bin/env python

import re
import sys

bash_name = re.sub(r'\.\d+\.py', '', sys.argv[0])

def process(s):
    print s[::2]

def main():
    if len(sys.argv) > 1:
        print "Usage: {0} <parameter>".format(bash_name)

if __name__ == "__main__":

chmod u+x $TFILE
./$TFILE "$@"
/bin/rm $TFILE


./ Your_Name
Yu_ae    # output
Categories: fun, python Tags: ,

call python in a shell script

March 5, 2012 Leave a comment

Floating point arithmetic in bash is problematic, expr supports integers only for instance.

Not an optimal solution but it works:

$ python -c "print 5.5*3"

$ num=`python -c "print 5.5*3"`
$ echo $num
Categories: python Tags: ,