echo escape sequences in bash shell

echo escape sequences in bash shell

Post by Adam Cannel » Fri, 21 Dec 2001 01:00:34



How do I use the echo esc. sequences within my scripts in the bash shell?
They don't seem to work at all.  Any help appreciated.....

(Please forgive me if infinitely stupid question)

Adam

 
 
 

echo escape sequences in bash shell

Post by Joe Halpi » Fri, 21 Dec 2001 02:38:56



> How do I use the echo esc. sequences within my scripts in the bash
> shell?  They don't seem to work at all.  Any help appreciated.....

What did you try, and what did it not do that you wanted it to?

Joe
--
Remember Flight 93

 
 
 

echo escape sequences in bash shell

Post by Willia » Fri, 21 Dec 2001 03:44:33



Quote:> How do I use the echo esc. sequences within my scripts in the bash shell?
> They don't seem to work at all.  Any help appreciated.....

Try:
    echo -e "This should\n  do it"

I use something like this at the top of my scripts:

if [ "x`echo \"\n\"`" = 'x\n' ] ; then
    EcHo="echo -e"
else
    EcHo="echo"
fi

Then whereever you'd use echo substitute $EcHo (or give
it your own name - I picked that for my own reasons).-Wm

 
 
 

echo escape sequences in bash shell

Post by Barry Margoli » Fri, 21 Dec 2001 04:50:26






>> How do I use the echo esc. sequences within my scripts in the bash shell?
>> They don't seem to work at all.  Any help appreciated.....

>Try:
>    echo -e "This should\n  do it"

Is there an ESCAPE character anywhere in there?  If not, how is it relevant
to this question?

The answer to the OP is that you have to put the literal escape sequence in
the string you're echoing.  The echo command doesn't support \e to
translate into the ESC character.

You might also look into the tput command, which makes use of
termcap/terminfo to send terminal-dependent control sequences.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

echo escape sequences in bash shell

Post by Matthew Land » Fri, 21 Dec 2001 06:46:14



> Is there an ESCAPE character anywhere in there?  If not, how is it relevant
> to this question?

> The answer to the OP is that you have to put the literal escape sequence in
> the string you're echoing.  The echo command doesn't support \e to
> translate into the ESC character.

> You might also look into the tput command, which makes use of
> termcap/terminfo to send terminal-dependent control sequences.

Depending on the UNIX used, one might use non builtin commands.  On AIX,
/bin/echo and /bin/printf both suppot printing escape codes via quoted
octals:

  /bin/echo "\033[44m" # for blue background of terminal

Also, awk can be used to print with escape chars the same way.

  echo |awk '{printf "\033[44m"}'

Using awk for your echo statement is overkill, but it does work.  :)

 - Matt
--
_______________________________________________________________________

  IBM High Speed Interconnect - Fibre Channel I/O Dev/Test/Support
   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

echo escape sequences in bash shell

Post by Chris F.A. Johnso » Fri, 21 Dec 2001 08:35:51



> How do I use the echo esc. sequences within my scripts in the bash shell?
> They don't seem to work at all.  Any help appreciated.....

> (Please forgive me if infinitely stupid question)

What type of escape sequences are you referring to?

The C-style sequences (\t, \n) are available with "echo -e", or printf (a
bash builtin as well as an external binary).

The ANSI screen sequences are obtainable with a literal ESC in the string
(use ^V ESC to enter it), or "echo $'\033'" or using \e with "echo -e" or
printf:

        echo " [31mHello [0m world"
        echo $'\x1b'"[43mHello"$'\x1b'"[0m world"
        echo -e "\e[45mHello\e[0m\tworld"
        printf "\e[34m%s\e[0m\t%s\n" Hello world

ANSI screen sequences may or may not work, depending on your terminal
type.

--

        =================================================================


 
 
 

echo escape sequences in bash shell

Post by Willia » Sat, 22 Dec 2001 04:44:58







> >> How do I use the echo esc. sequences within my scripts in the bash
shell?
> >> They don't seem to work at all.  Any help appreciated.....

> >Try:
> >    echo -e "This should\n  do it"

> Is there an ESCAPE character anywhere in there?  If not, how is it
> relevant to this question?

Yes, there is: "\" (at least the Nutshell book calls it an "escape
character", repeatedly). Since the OP refers to "the echo esc."
sequences, AND was having trouble with echo under bash (which does
require -e to recognize these sequences), it's a reasonable
assumption.-Wm