Python: How to get some bash commands in your interactive Python shell.

Submitted by john on Tue, 03/21/2017 - 16:04

Python LogoDon't you wish you could remember what the text file name was without getting out of your shell?

If you could just do a quick ls that would be great. Well, you can, but, not without a little bit of work. 

First off, Do yourself a favor by putting this line in your .bash_profile
export PYTHONSTARTUP=/home/myuser/

Now lets create our script to give us the commands we want.


#!/usr/bin/env python

import sh

ls ='laGh')
pwd = sh.pwd()
clear = sh.clear()

You get the idea. You can add all the commands you want to use. If you want more advanced commands then set up aliases using subprocess and turn shell on. Then you can use pipes, redirects, sed, awk, grep and so on. 

Be sure to install sh with pip or easy_install.

Now restart your terminal and type in python.

You should be able to type ls or clear or whatever commands you set up and they will work.