execute bash builtins in python

alex goretoy agoretoy at gmail.com
Fri Mar 12 13:57:47 EST 2010


Steve thank you. The problem is that you can only run commands from Popen or
os.system and stuff. You cant run bash shell builtin commands for some
reason.

I was able to get this to work. What I did is call this:

Popen(["bash -c 'source
$HOME/.bashrc;alias'"],shell=True,stdout=PIPE).stdout.read()

and make sure you don't have a line like this in your .bashrc:
[ -z "$PS1" ] && return

Another approach I came up with was to set aliases and functions as a
environment variable and access them with os.environ.

this is how I'm doing it.

export ALIASES=`alias|awk '{print $2}'|cut -d = -f 1|xargs`
export FUNCTIONS=`declare -F|awk '{print $3}'|xargs`

use this at the end or the .bashrc and then you can access defined aliases
and functions inside python with environ

-Alex Goretoy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100312/b648b89f/attachment-0001.html>


More information about the Python-list mailing list