How to display a color message from a bash script?
I found this example:
echo -e "This is red->\e[00;31mRED\e[00m"
It works if you execute direct, from the command line, bu, if you create a file like:
#! /usr/bin/sh echo -e "This is red->\e[00;31mRED\e[00m"
Does not work. How to fix? Or is it possible in bold? Please do not use Lua, which it did not install.
Change This could be your problem (probably):
#!/bin/bash echo -e "This is red->\e[00;31mRED\e[00m"
The reason is that sh
does not have a built-in echo
command that supports escape sequences.
Alternatively, you can reference the script as
bash ./myscript.sh
<sub>
: backgrounders@dcaf.ch
ANSI escape sequences are interpreted by the terminal.
If you run in pipe / with a redirected IO, it will not exit to the terminal, so the screens will not be interpreted.
Tips:
- see
ansifilter
for a tool that can filter ANSI escape sequences (and possibly translate to HTML and others) use GNU less, for example. to get ANSI screens working in a pager:
grep something --colour=always files.* | less -R
Or just as I do
# also prevent wrapping long lines alias less='less -SR'
sub>