Echo a colored text with tabs

I have the line below to echo values with tab between them. The text is also colored, however, some\t does not work.

The output of this one below will have the first two \t not working.

echo "\033[1;31m${var[a]}\t$time\t$end\t$day\t$score\033[m"

This one below will have all the \t working but will also print -e in the screen.

echo -e "\033[1;31m${var[a]}\t$time\t$end\t$day\t$score\033[m"

echo is implementation dependant, i.e. what OS and shell you are using. Some accept the -e option, other don't. Check your man pages.
It might be better to use printf for those printouts as it is standardized across platforms and shells.

Expanding on what RudiC has already said...

As has been said in these forums MANY times before; the behavior of echo , when invoked with any argument containing a backslash ( \ ) or when the first argument starts with a hyphen ( - ), varies from system to system and shell to shell. And, using terminal escape sequences to set text color varies from terminal (or terminal emulator) to terminal (or terminal emulator). Without knowing what the expansions of the variables in the arguments to your echo command expand to, what shell you're using, what operating system you're using, and how your environment has been initialized, it is hard to guess at what might be going wrong.

The portable way to write those escape sequences to a terminal (or terminal emulator) from a shell is to use printf instead of echo :

printf '\e[1;31m%s\t%s\t%s\t%s\t%s\e[m\n' "${var[a]}" "$time" "$end" "$day" "$score"

And, of course how your terminal or terminal emulator responds to those escape sequences can't be determined from the information you have provided. (But, on many terminals that recognize ANSI terminal escape sequences, it will produce red text).

It might be worth a try to query the termcap database (or its replacement terminfo, whatever there is) prior to using escape sequences. For instance:

typeset UnderOn="$(tput sgr 0 1)"    # turn off standout, turn on underline
typeset UnderOff="$(tput sgr 0 0)"   # turn off standout, turn off underline

print - "normal Text ${UnderOn}underlined part${UnderOff} normal again"

I hope this helps.

bakunin

1 Like

OSX 10.7.5, default terminal using sh...
Using /bin/echo not the builtin...
You could try:-

Last login: Sun Jun 14 16:03:15 on ttys000
AMIGA:barrywalker~> sh
AMIGA:barrywalker~> TAB=$'\011'
AMIGA:barrywalker~> BEEP=$'\007'
AMIGA:barrywalker~> TEXT="Hello World."
AMIGA:barrywalker~> /bin/echo "$TAB$TEXT$BEEP"
	Hello World.
AMIGA:barrywalker~> exit
exit
AMIGA:barrywalker~> exit
logout

[Process completed]

EDIT:
Added a beep for fun...