-1

I am trying to write a python script that runs a command through bash.

subprocess.Popen(cmd, shell=True, executable='/bin/bash')

The script is working fine except that it does not source my bash aliases or other configs. How do I include them ? Is there a better way of doing this in python ?

  • You should take a look at [this question/answer](http://stackoverflow.com/questions/1615877/why-aliases-in-a-non-interactive-bash-shell-do-not-work) for information about how to enable aliases for non-interactive shells. – mshildt Jan 20 '14 at 21:39

1 Answers1

1

Execute bash with the --init-file argument. From the bash manpage:

   --init-file file
   --rcfile file
          Execute  commands  from file instead of the system wide initial‐
          ization file /etc/bash.bashrc and the standard personal initial‐
          ization  file ~/.bashrc if the shell is interactive (see INVOCA‐
          TION below).
Jakub Kotowski
  • 7,411
  • 29
  • 38
  • How do I pass arguments to the shell executable ? Using `executable='/bin/bash --init-file /home/user/.bash_aliases'` gives `No such file or directory` – fiery_falcon Jan 20 '14 at 21:33
  • you pass in a list of arguments `Popen(['/bin/sh', '-c', args[0], args[1], ...])` it's described in the documentation: http://docs.python.org/2/library/subprocess.html#subprocess.Popen – Jakub Kotowski Jan 20 '14 at 21:42
  • otherwise the whole string in your `executable` is interpreted as the application to find and execute and of course there's no such application – Jakub Kotowski Jan 20 '14 at 21:44