0

Is there a way to let the terminal show the raw escape sequence? (e.g. those used to control color)

I came across this UNIX.SE question, and want to filter "red" output ( Filter output of command by color)

However, the commands in the answers do not work for my output (e.g. the red in git status for "Untracked files"). They do work for the examples there echo -e '\033[00;31mtest\033[00m' | grep --color=none '[[:cntrl:]]'. Filtering with grep --color=none '[[:cntrl:]]' works on my output but also shows text with other colors.

I guess the red I see must be a different red than \e[31m.

Is there a way to let terminal output raw escape sequences? (so that I can use the right sequence to filter the specific red color I see)

(bash, Ubuntu 18.04)

-- Update --

For clarification, my question is on how to show the raw escape sequence.

@egmont' answer using script worked. Basically, I just needed to use script to record the output and use one of the answers to the above question to find the red color text, e.g.:

script /tmp/a.txt
git status
exit
cat /tmp/a.txt | grep -Eo $'\e\\[31m[^\e]*\e\\[[03]?m'

The other answer/comments are also correct in pointing out that my usage of git didn't get the color through the pipe.

  • git status only produces color when its output is to a terminal, not in a pipeline. This is the case for most commands actually. So if you're trying to filter one of these commands by color, you'll first have to convince it to send the color into the pipeline – Fox Feb 22 at 4:43
2

Not sure if I understand your question correctly.

I think you want to examine what a certain utility (like git status) sends to the terminal emulator. Am I right?

Start up the script utility, it makes a log of exactly this. Then call git status or whatever you want. Then exit from script's shell, it'll notify you that a file called typescript has been created. Open this file in a text editor to find the sequences you're looking for.

4

I guess the red I see must be a different red than \e[31m.

You guess wrongly.

% ptybandage git status . | grep rules | console-decode-ecma48
TAB
SGR 31
'm'
'o'
'd'
'i'
'f'
'i'
'e'
'd'
':'
' '
' '
' '
'r'
'u'
'l'
'e'
's'
SGR 0
CR
LF
% git status . | grep rules | console-decode-ecma48
TAB
'm'
'o'
'd'
'i'
'f'
'i'
'e'
'd'
':'
' '
' '
' '
'r'
'u'
'l'
'e'
's'
LF
% 

You haven't made git think that it is writing to a terminal, so it isn't producing colour.

Further reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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