Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am writing software that interacts with an embedded device running a version of busybox Linux. Basically, I am just sending commands to the device and parsing the output from the commands . The commands are executed either on the linux shell directly or on the command line of an in-house CLI application running on the device.

Everything works fine except that the output is peppered with terminal control escape sequences. On terminal applications such as teraterm or putty, these escape sequences do useful things like color errors red and other nice features for a pleasant user interface.

The problem is that I have to programmatically parse the output from the commands and account for things like "(esc)[2k" in the output.

For example, a typical send/receive interaction where I send a command, "my-cmd" would go like this...

[send] my-cmd
[receive] my-cmd <esc>[2Kprompt> my-cmd
output of the command
prompt> 

What I would really like to do is to turn off these escape sequences. Is this something that can be done on the command-shell at the beginning of a session? Or is there no way around this other than filtering the output?

share|improve this question
1  
There is probably a better way, but you might be able to do this on a per-command basis. What are the commands you are sending? I am thinking about things like ls being an alias for ls --colors=tty and grep pointing to grep --color and suchlike. You might be able to avoid the escape sequences if you use, for example, /bin/ls instead of ls. –  terdon Jan 28 '13 at 21:35
1  
@terdon there is a simpler way. simply use \ls instead of ls and it will bypass all aliases. –  mnmnc Jan 28 '13 at 21:43
    
@mnmnc nice one, I did not know that, thanks! –  terdon Jan 28 '13 at 21:47
1  
I just learned about this a half an hour ago but it is actually a way of quoting. A quoted command name causes BASH to ignore any aliases. It should work with all aliases not all commands but most cases where you have color output without specifying an option are aliases. Have a look here for some more info. –  terdon Jan 28 '13 at 22:26
1  
Redirecting output (e.g. through cat, into a file or a named pipe) might also work. You'll also get full (untruncated) output from commands such as ps aux that frequently suffer from screens not being wide enough. –  Daniel Beck Jan 28 '13 at 23:34

2 Answers 2

up vote 3 down vote accepted

Most (if not all) cases where a *nix command prints colored output without the user specifically asking for it with an option involve aliases. In fact, many Linux distributions include an alias for ls and grep specifying colors in the global /etc/bash.bashrc.

These are from my Linux Mint Debian Edition:

$ grep alias /etc/bash.bashrc
    alias ls='ls --color=auto'
    alias grep='grep --colour=auto'

So, if your program is calling these commands through BASH, you are running ls --color=auto instead of ls.

You can bypass aliases in BASH (maybe other shells too but I have not tried it) by the following methods (taken from here):

  1. the full pathname of the command: /bin/ls

  2. command substitution: $(which ls)

  3. the command builtin: command ls

  4. double quotation marks: "ls"

  5. single quotation marks: 'ls'

  6. a backslash character: \ls

If you use one of these methods in your software you should not have to worry about escape sequences.

share|improve this answer

if you're on redhat or a relative, like fedora or centos, you might look at
modifying

/etc/sysconfig/init

look for the lines that say something like:

 # anything else => new style bootup without ANSI colors or positioning

 BOOTUP=color

and like it says, change the variable to anything other than "color"

this affects behavior of some of the system command line utilities, like service, ifup, ifdown, etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.