4

I found this example:

echo -e "This is red->\e[00;31mRED\e[00m"

It works if execute direct, from command line, bu if create file like:

#! /usr/bin/sh
echo -e "This is red->\e[00;31mRED\e[00m"

Doesn't work. How to fix? Or may be possible output in bold? Please don't use Lua it doesn't installed.

2 Answers 2

7

Edit This might be your problem (likely):

#!/bin/bash
echo -e "This is red->\e[00;31mRED\e[00m"

The reason is that sh doesn't have a builtin echo command, that supports escapes.

Alternatively you might invoke your script like

bash ./myscript.sh

Backgrounders

ANSI escape sequences are interpreted by the terminal.

If you run in a pipe/with IO redirected, ouput won't be to a terminal, hence the escapes don't get interpreted.

Hints:

  • see ansifilter for a tool that can filter ANSI escape sequences (and optionally translate to HTML and others)
  • use GNU less, e.g. to get ANSI escapes working in a pager:

    grep something --colour=always files.* | less -R
    

Or simply, as I do

# also prevent wrapping long lines
alias less='less -SR'

Sign up to request clarification or add additional context in comments.

2 Comments

I cannot use ansifilter - I can use only Bash
Spotted it! You are not using bash. Edited answer
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.