1

I have the following code that works great to run the ls command. I have a bash alias that I use alias ll='ls -alFGh' is it possible to get python to run the bash command without python loading my bash_alias file, parsing, and then actually running the full command?

import subprocess

command = "ls"  # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)

#Launch the shell command:
output = process.communicate()

print (output[0])

Trying with command = "ll" the output I get is:

/bin/sh: ll: command not found
b''
5
  • Possible duplicate: stackoverflow.com/questions/12060863/… Commented Mar 9, 2016 at 19:42
  • I don't want to be obnoxious, but why are you using subprocess to list files? Wouldn't import os os.listdir() achieve what you want? Commented Mar 9, 2016 at 19:44
  • 1
    Please note alias' are a shell thing, not a bash thing, so, your system has no knowledge of the alias. You can have the command execute through a shell, but that will still result in your alias file being parsed (when the shell loads it will parse it) Commented Mar 9, 2016 at 19:44
  • My actual command is piping commands into a docker container, this was just an example to break the problem down Commented Mar 9, 2016 at 19:44
  • 1
    Also, this doesn't answer your question, but you can of course do subprocess.Popen(['ls', '-alFGh'], stdout=subprocess.PIPE, stderr=None) instead (especially given shell=True is generally disuaded). Commented Mar 9, 2016 at 19:47

1 Answer 1

1

You cannot. When you run a python process it has no knowledge of a shell alias. There are simple ways of passing text from parent to child process (other than IPC), the command-line and through environment (i.e. exported) variables. Bash does not support exporting aliases.

From the man bash pages: For almost every purpose, aliases are superseded by shell functions.

Bash does support exporting functions, so I suggest you make your alias a simple function instead. That way it is exported from shell to python to shell. For example:

In the shell:

ll() { ls -l; }
export -f ll

In python:

import subprocess

command = "ll"  # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)

output = process.communicate()

print(output[0].decode())    # Required if using Python 3

Since you are using the print() function I have assumed you are using python 3. In which case you need the .decode(), since a bytes object is returned.

With a bit of hackery it is possible to create and export shell functions from python as well.

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! This is very very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.