Aliases are defined and interpreted by the shell. However, when using Python's subprocess
module, the shell is typically executed non-interactively, meaning that alias files like .profile
or .bashrc
won't be read, making aliases unavailable within the subprocess
call.
To execute an alias within a subprocess.call
call, you can set the shell
keyword to True
, as seen below:
subprocess.call("myAlias", shell=True)
This informs Python to execute the command through the shell, enabling access to shell features like filename wildcards, shell pipes, and environment variable expansion. However, it's important to note that the shell (/bin/sh) used by subprocess
is non-interactive by default, preventing it from reading alias definitions from startup files.
To address this, you can use the $ENV
environment variable to force the shell to read the alias file:
subprocess.call("myAlias", shell=True, env=dict(ENV='/path/to/aliasfile'))