Echo a colored text with tabs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo a colored text with tabs
# 1  
Old 06-13-2015
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.
Code:
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.
Code:
echo -e "\033[1;31m${var[a]}\t$time\t$end\t$day\t$score\033[m"

# 2  
Old 06-13-2015
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.
# 3  
Old 06-13-2015
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:
Code:
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).
# 4  
Old 06-13-2015
Quote:
Originally Posted by Don Cragun
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:

Code:
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
This User Gave Thanks to bakunin For This Post:
# 5  
Old 06-14-2015
OSX 10.7.5, default terminal using sh...
Using /bin/echo not the builtin...
You could try:-
Code:
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...

Last edited by wisecracker; 06-14-2015 at 12:09 PM.. Reason: Added BEEP for fun...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Change text color from echo command?

I have a bash script that starts and stops a game among other things through in.fifo and out.fifo In game the text comes out gray . Kinda hard to see in game window . I would like to change it to purple and maybe capitalize it. #!/bin/bash #nwservctl.sh cd... (5 Replies)
Discussion started by: 222222quick
5 Replies

2. Shell Programming and Scripting

Using echo, grep and wc and outputing to text file

My current line command is as follows: echo -n "text: " ; grep "blah text" ../dir1/filename | wc -l The output to the screen is as needed, but how do I print to a text file? (9 Replies)
Discussion started by: ncwxpanther
9 Replies

3. Shell Programming and Scripting

Echo variable contents into a text file...

Hi all, I am trying to create a script to read my Windows UUIDs and create mounts in fstab. I know there are different and maybe even better ways to mount Windows partitions at boot time, but I always manually create them in fstab successfully. I do a clean install of Ubuntu often and would like to... (2 Replies)
Discussion started by: Ian Pride
2 Replies

4. Shell Programming and Scripting

Problem using echo command for text starting with /

Hi, i need to print following text using echo: /abc dir/c\ so i tried echo "/abc dir/c\ But it gives me error of Incorrect usage, i am using Hamilton cshell in windows Vista. Can any one please help me. Thanks in advance Sarbjit (3 Replies)
Discussion started by: sarbjit
3 Replies

5. Shell Programming and Scripting

Echo Variables and Text

I've been trying to get the syntax right so I can echo a $var and then text around it or after it. It either wont display text or $var or one overwrites the other at the beginning of the line. Trying to do something like this. var=1 echo $var"+1.1" #output expected 1+1.1 Its an older... (3 Replies)
Discussion started by: Grizzly
3 Replies

6. Shell Programming and Scripting

Help with replacing tabs inside "" with some text/blank

I am poor with scripting;) I have a file in the following format; 'This is a "test in production" of importance.' I want to get rid of the spaces inside the "" part only to get the output as, 'This is a "testinproduction" of importance.' (1 Reply)
Discussion started by: shmathew
1 Replies

7. Shell Programming and Scripting

Insert Tabs / Indent text

Hi, i need replace the slash (/) with a newline (\n) and a tab (\t). With 'find -type f' in a folder i got this output: ./1999/01/file1 ./1999/01/file2 ./1999/02/file1 ./2000/04/file1 ./2000/04/file2 ./2000/04/file3 ./2000/04/file4 ./2000/06/file1 ./2000/06/file2 ./2000/06/file3... (8 Replies)
Discussion started by: Tonda
8 Replies

8. Shell Programming and Scripting

sh: Inserting tabs and moving text to 1 line

I trying to extract certain text from a csv file and then placing it into another csv file, but having problems getting the data to placed in one line with tab separated fields. Basically would like to have text sent to interfaces.csv in one line seperated by tabs. As it currently places files... (6 Replies)
Discussion started by: 00000008
6 Replies

9. UNIX for Dummies Questions & Answers

is it possible to echo 'some text' every 10 minutes on my screen

is it possible to echo 'some text' every 10 minutes on my screen continues , without cron. (1 Reply)
Discussion started by: vkandati
1 Replies

10. Shell Programming and Scripting

blink / flash echo text in a menu

I have a ksh script that is a login menu for my end users. I would like to have one line of the welcome message flash when there is a system notice about an impending outage. The welcome message is a series of echo statements which prints a text file "msg of the day" with the status msg. I tried... (1 Reply)
Discussion started by: MizzGail
1 Replies
Login or Register to Ask a Question