New task #5722 in pdksh: build-in echo broken

Jakub Bogusz qboosh at pld-linux.org
Wed Nov 2 22:53:37 CET 2005


On Wed, Nov 02, 2005 at 06:16:34PM +0100, Jacek Konieczny wrote:
> On Wed, Nov 02, 2005 at 02:38:37PM +0100, Jakub Bogusz wrote:
> > (updated)
> > > pdksh$ echo '\'
> > > \
> > > pdksh$ echo '\\'
> 
> of course, it should be:
> pdksh$ /bin/echo '\\'
> 
> > > \\
> > 
> > And it conforms to POSIX/SUSv2 specs.
> 
> I was not sure, that is why I have reported it instead of trying to
> 'fix' it.
> 
> > > bash$ echo '\\'
> > > \\
> > > bash$ /bin/echo '\\'
> > > \\
> > > 
> > > This pdksh behaviour may break some scripts and e.g. breaks
> > > slony1_dump.sh script from slony1-tools package.
> > 
> > So scripts are broken.
> 
> Any idea how to fix the script in a portable way, so it outputs
> a defined number of backslashes?

Recommended way is to use printf.
Working (at least in pdksh, ash, bash, zsh) but still non-portable acc.
to SUSv2) solution is to always use "echo -e" and escape backslashes.

#v+
     APPLICATION USAGE

     It is not possible to use echo portably across all systems that are not XSI-conformant
     unless both -n (as the first argument) and escape sequences are omitted.

     The printf utility can be used portably to emulate any of the traditional behaviours of the
     echo utility as follows:

       * The XSI echo is equivalent to:


 printf "%b\n" "$*"


       * The BSD echo is equivalent to:


 if [ "X$1" = "X-n" ]
 then
     shift
     printf "%s" "$*"
 else
     printf "%s\n" "$*"
 fi


     New applications are encouraged to use printf instead of echo.
#v-

> And bash and our /bin/echo are broken too?

"not XSI-conformant"


-- 
Jakub Bogusz    http://qboosh.cs.net.pl/



More information about the pld-devel-en mailing list