0

The file below, which I've called /tmp/ltSortedList, when sourced in tcsh, outputs lines in different colors:

echo "\033[0;34m  R:0         2020  12  10  18:53  regularFile "\\e[0m
echo "\033[0;34m  R:32325978  2020  12  10  17:10  G670_UserGuide_Long.pdf "\\e[0m
echo "\033[0;31m  S:13        2020  12  09  18:49  /tmp/linkTarget "\\e[0m

But when I source it from bash (or use .) the coloring doesn't work (see figure). Presumably it has something to do with the control codes.enter image description here

Could somebody please explain how to get the same result in bash as I get in tcsh?

Thanks for any advice

2 Answers 2

2

My guess is that in both tcsh and bash you're getting a built-in implementation of echo; tcsh's implementation is interpreting the escape sequences and bash's is not.

Try this:

/bin/echo -e "\033[0;34m  R:0         2020  12  10  18:53  regularFile "\\e[0m
/bin/echo -e "\033[0;34m  R:32325978  2020  12  10  17:10  G670_UserGuide_Long.pdf "\\e[0m
/bin/echo -e "\033[0;31m  S:13        2020  12  09  18:49  /tmp/linkTarget "\\e[0m

That will make sure you use the /bin/echo binary. The -e flag will cause /bin/echo to interpret the escape characters.

1
  • On a 100% POSIX platform, -e is echoed itself and escape sequences are always expanded. This is e.g. how bas is compiled in Solaris and MacOS
    – schily
    Jan 31, 2021 at 17:27
0

I can't speak to tcsh. In bash, echo is builtin, /bin/echo is not, but the crucial point is the -e. Both echo and /bin/echo will do the color changing with it, without it will not.

1
  • Except if he's sourcing the same file in both shells, tcsh's builtin echo might not support -e. Explicitly using /bin/echo makes sure to use the same tool with the same behavior across both shells. Dec 11, 2020 at 20:33

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.