xargs echo colored output

1.1k views Asked by At

I have the following command:

somethingRegex | xargs -I {} sh -c 'echo -e "found \e[34m{}\e[39m";dummy {}'

The color part of the echo does not work, example output:

-e found \e[34mresult\e[39m
dummy output
repeat

A plain echo does work with {} being nice blue

echo -e "found \e[34m{}\e[39m"

How do I fix this?

1

There are 1 answers

0
Thomas Dickey On BEST ANSWER

Perhaps this is for Linux (although OSX adds an interesting twist by reversing the roles of bash and echo).

Linux's /bin/echo has a -e option which expands escapes of the sort you show, while some shells (such as dash, used in Debian) follow POSIX more closely, and do not do this. The find program acts as if it runs /bin/sh, which may not be your actual shell. Debian uses dash as /bin/sh.

Likewise, older versions of bash (my local OSX server has 3.2.53) do not support the -e option, while newer ones (checking my local Debian with 4.1.5) do support the -e option.

Since all of that behavior is non-standard, the usual recommendation is to use the printf utility, which also provides non-standard features on Linux, but the parts that you need will be portable enough:

somethingRegex | xargs -I {} sh -c 'printf "found \033[34m{}\033[39m";dummy {}'