Possible Duplicate:
Piping data to Linux program which expects a TTY (terminal)
I want to display colors from script that is non interactive and I need to tell the shell that in fact my script is a terminal that support colors so command like ls --color=auto will display colors (ls have option --color=always but I would like to support for all posible commands that support colors as well).
I call my shell via python Popen. I’ve try to set TERM environment variable but It don’t work.
subprocess.Popen(['/bin/bash', '-c', command],
env={'TERM':'xterm-color'},
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Is there a way to tell bash that I’m a terminal?
Programs normally determine automatically (
ls --color=auto) if there’s a terminal connected. That goes by checking stdout/err file descriptors.In case of you piping to and from python (as you are doing in your above example), programs will have their stdout/err connected to a pipe, not a terminal, and thus will normally not output color codes.
Try to redirect the process’ stdout/err directly to the attached terminal, not back to your python app. If that’s not feasible (because you need the output in python–but in this case you normally don’t want color codes anyway), you’ll have to fiddle around setting up a pseudo terminal in python somehow.