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?
Perhaps this is for Linux (although OSX adds an interesting twist by reversing the roles of
bash
andecho
).Linux's
/bin/echo
has a-e
option which expands escapes of the sort you show, while some shells (such asdash
, used in Debian) follow POSIX more closely, and do not do this. Thefind
program acts as if it runs/bin/sh
, which may not be your actual shell. Debian usesdash
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: