*BSD News Article 62789


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!bunyip.cc.uq.oz.au!munnari.OZ.AU!news.ecn.uoknor.edu!news.ysu.edu!odin.oar.net!malgudi.oar.net!imci4!newsfeed.internetmci.com!howland.reston.ans.net!Germany.EU.net!zib-berlin.de!irz401!uriah.heep!news
From: j@uriah.heep.sax.de (J Wunsch)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Bug in ECHO?
Date: 22 Feb 1996 20:35:23 GMT
Organization: Private BSD site, Dresden
Lines: 115
Message-ID: <4gik2b$pmm@uriah.heep.sax.de>
References: <4famj5$4hf@complete.org> <4gb0lt$d34@uriah.heep.sax.de> <Dn45F4.LpB@theatre.pandora.sax.de>
Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
NNTP-Posting-Host: localhost.heep.sax.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: knews 0.9.3

mw@theatre.pandora.sax.de (Martin Welk) writes:

> >The echo command in *BSD is not supposed to expand backslashes.  It
> >should echo its argument, not interpret them.
> >
> >The functionality you are looking for can be found in printf(1).
> 
> Unfortunately, this is not directly available in shell scripts.

Why not?

> Sometimes it's a kind of philosophy if people like bash or not, but
> at least, it works using the internal echo command - I even get more
> functionality with it. I didn't want to convert everything to perl
> or something like that, so using bash was the easier way for me.
> And it works good, although performance could be better.

First, that's what Posix says:

 4.19 echo - Write arguments to standard output                        475

    string      A string to be written to standard output.  If the first
                operand is "-n" or if any of the operands contain a
                backslash (\) character, the results are implementation
                defined.

So you cannot rely on it, either.

However, you can rely on printf(1) doing this (even though the joint
subset of BSD and SysV printf is not very big -- BSD's behaves almost
like printf(3), SysV's does only handle strings).

     (3)  In addition to the escape sequences shown in Table 2-15 (see
          2.12), \ddd, where ddd is a one-, two-, or three-digit octal
          number, shall be written as a byte with the numeric value
          specified by the octal number.

And that's table 2-15:

                      Table 2-15  -  Escape Sequences
 _________________________________________________________________________
  Escape       Represents
 Sequence       Character                       Terminal Action
 _________________________________________________________________________

    \\      backslash                None.
    \a      <alert>                  Attempts to alert the user through
                                     audible or visible notification.
    \b      <backspace>              Moves the printing position to one
                                     column before the current position,
                                     unless the current position is the
                                     start of a line.
    \f      <form-feed>              Moves the printing position to the
                                     initial printing position of the next
                                     logical page.
    \n      <newline>                Moves the printing position to the
                                     start of the next line.
    \r      <carriage-return>        Moves the printing position to the
                                     start of the current line.
    \t      <tab>                    Moves the printing position to the
                                     next tab position on the current
                                     line.  If there are no more tab
                                     positions left on the line, the
                                     behavior is undefined.
    \v      <vertical tab>           Moves the printing position to the
                                     start of the next vertical tab
                                     position.  If there are no more
                                     vertical tab positions left on the
                                     page, the behavior is undefined.
 _________________________________________________________________________

Finally, the bash doesn't work for me either as you expect it:

j@uriah 613% bash
bash$ echo "a\tb\n"
a\tb\n
bash$ exit
j@uriah 614% ksh
$ echo "a\tb\n"
a       b

$ j@uriah 615% 
j@uriah 615% sh
$ printf "a\tb\n"
a       b
$ j@uriah 616% 

Finally, a SVR4.2 (you've seen the machine today, btw. :):

j@smiley 101% sh
$ echo "a\tb\n"
a       b

$ echo -n "a\tb\n"
a       b
$ j@smiley 102% 
j@smiley 102% ksh
$ echo "a\tb\n"
a       b

$ echo -n "a\tb\n"
a       b
$ printf "a\tb\n"
a       b
$ 

As you can see, the SysV does even recognize the -n option, but it
does also behave Posix-correctly for the printf.

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)