Reputation: 105
In my script I have somewhat following lines:
str=$(command)
Then I want to echo output of a command using red color:
echo -e "\e[31m$str\e[0m"
But it continues to print it in default color.
Found interesting thing: when I add --color
to the end of my command it DOES print command output in red color, but it says following:
Invalid command line options. Usage:
kes4lwks-control [-L] --show-license-info <file>
Is there any chance to print output of that command in color?
If you need, I'm executing next command:
/opt/kaspersky/kes4lwks/bin/kes4lwks-control --show-license-info <path-to-lic-file>
UPDATE:
My above code perfectly works, when command executes with out errors (returns 0), but when it says, for example,
Error, couldn't show infromation on the license: signature of license key is invalid.
Echoed text is not colored. May be I need something to do with stderr descriptor?
Upvotes: 5
Views: 4056
Reputation: 241731
You are confusing yourself by not distinguishing between normal program output to stdout and error messages output to stderr.
Executing
str=$(command)
causes command to be executed with its stdout redirected, but without changing stderr. The consequence is that error messages sent to stderr will just appear immediately on stderr, which is presumably still your terminal.
The redirection is to a bash process which captures the output (that is, to stdout) and, when the command is finished, assigns the collected output to the shell variable. If there was an error and nothing was sent to stdout, the shell variable will end up set to an empty string.
So
str=$(command)
printf '\033[31m%s\033[0m\n' "$str"
will directly output error messages, and will capture and later output regular output. (I changed the echo
to printf
and \e
to \033
in order to make the command portable. Also see note 1, below.)
If you just want to colour output, there is no need to capture the output in a variable at all. Just send the appropriate colour sequences before and after executing the command:
printf '\033[31m'
command
printf '\033[0m'
That will colour all output from command. (Of course, the output itself could include colour sequences, but there is no simple way to get around that.)
If you have some other reason to capture the output (which seems unlikely), you could run the command with stderr redirected to stdout. In that case, you will get both stdout and stderr output (intermingled) in the shell variable:
str=$(command 2>&1)
Although using ANSI colour sequences is probably going to work in any modern platform on which bash is running, some people will recommend the use of the tput
command in order to access the terminfo
database. For example,
tput setaf 1 # Set terminal to ANSI colour 1 (red)
command
tput sgr0 # Reset all terminal attributes
tput setaf 0
is not the same as the control sequence ESC [ 0 m
. setaf 0
sets the foreground colour to black, which will make output invisible if you use a white-on-black console, while ESC [ 0 m
resets all character display attributes to their defaults. That's also what tput sgr0
is defined as doing, although it may also reset other terminal attributes. (With my terminfo setting, tput sgr0
outputs ESC ( B
followed by ESC [ m
; the former resets the terminal font mapping, which I think is redundant in my case. But YMMV.)
It is possible (but unlikely) that there is no setaf
entry for your terminal, in which case you are supposed to fall back to setf
; however, the colour numbering is different so that setf 1
is blue and setf 4
is red, while setaf 1
is red and setaf 4
is blue. All of these convoluted details are documented in man 5 terminfo
. Search for "Color Handling" and enjoy. If you are using Linux, you might also find man 4 console_codes
interesting reading.
Upvotes: 3