Color Linux Command Output

How to change color of bash output?

In order to print a single line, you need to unset the color that has been set. Using tput

$ echo "$(tput setaf 4)Print Blue$(tput sgr 0)"

How to enable colored output from shell command wrapped in a script?

To force color output from grep:

grep --color=always

From man grep:

   --color[=WHEN], --colour[=WHEN]
Surround the matched (non-empty) strings,
matching lines, context lines, file names, line
numbers, byte offsets, and separators (for fields
and groups of context lines) with escape
sequences to display them in color on the
terminal. The colors are defined by the
environment variable GREP_COLORS. The deprecated
environment variable GREP_COLOR is still
supported, but its setting does not have
priority. WHEN is never, always, or auto.

Bash is it possible to echo in color output of a command stored in a variable?

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)

Notes

  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.

Is there a way to preserve ANSI control codes in PowerShell command output?

To add to your own answer:

  • On Windows I'm not aware of any simple solutions, though perhaps there is a programmatic way to do what the script utility does on Unix-like platforms (see below).

  • On Unix-like platforms, you can use the script utility to make external programs believe they are connected to a terminal and thus make them produce colored output they would otherwise suppress:

    • script is not a POSIX-mandated utility, but it ships with at least some Linux distros, notably Ubuntu, as well as with macOS.
    • Notably, the macOS and Linux implementations use different syntax, as shown below.

Example:

  • Note:

    • ls --color=auto is used as a readily available test command, because it exhibits the conditional coloring behavior.

    • ls --color=always would exhibit unconditional coloring behavior, and would therefore obviate the need for script - as such, ls is an example of a utility that does allow you to request unconditional coloring; ls --color=auto merely serves as a stand-in for utilities that do not.

    • The variable assignment below ($out = ...) is enclosed in (...) in order to pass the value being assigned through, so you'll see right away that the coloring is preserved.

  • Linux

($out = script -qc 'ls --color=auto')
  • macOS:
($out = script -q /dev/stdout ls --color=auto)


Related Topics



Leave a reply



Submit