\e[00;31mRED\e[00m" It works if you execute di...">

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.

+4
source share
2 answers

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>

+7
source

Use the echo program, not the built-in echo command:

 #!/bin/sh MYECHO="`which echo`" if <test-whether-MYECHO-empty-and-act-accordingly> ... ... $MYCHO -e "This is red->\e[00;31mRED\e[00m" 
+1
source

Source: https://habr.com/ru/post/1383581/


All Articles