execute bash builtins in python

alex goretoy agoretoy at gmail.com
Fri Mar 12 19:57:47 CET 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.html>


More information about the Python-list mailing list