[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [semi-OT] How to echo \\ in dash

On Friday 28 August 2009 13:20:24 Paul E Condon wrote:
> On 2009-08-28_10:02:50, Boyd Stephen Smith Jr. wrote:
> > On Friday 28 August 2009 08:54:50 Johannes Wiedersich wrote:
> > > Hi list!
> > >
> > > In an effort to write my scripts in a posix conform way, I try to use
> > > dash instead of bash. One issue I don't understand:
> > >
> > > Why does 'echo \\\\' behave different for bash and dash?
> >
> > Per the POSIX and SUS specifications, the echo command, which maybe be
> > built into the shell, may apply C-sytle escapes ("\\", "\t", "\n", etc.)
> > to the string it receives from the shell.
> >
> > Typing 'echo \\\\' at the command line or in a script sends the argv = {
> > "echo", "\\" } to the echo binary/built-in.  At that point, a POSIX/SUS
> > conforming echo can output either "\" followed by a newline OR "\\"
> > followed by a newline.
> >
> > As a user/developer, it's a pain to deal with all the flexibility of
> > implementation that POSIX/SUS gives the "echo" command.  Instead, use the
> > printf command which is much better specified.  Just remember to end the
> > printf string with "\n" if you want a final newline.
> After reading this, I would say that 'echo', without option '-e' is a
> bashism that should have been flagged. In my scripts, there are
> multiple instances of plain 'echo'. None of them were flagged. I
> wonder what else is missing ...

Echo *with* the -e option is also non-portable.  Per POSIX/SUS, echo is not 
required to recognize any options. It *may* recognize the '-n' option, but 
only if it is the first argument.  On XSI systems (anything purported to be 
UNIX, IIRC), it *must not* recognize the '-n' option and *must* interpret 
specific C-style escapes in all it's arguments.

Here's a table (uses fixed-width font to view):

              POSIX                          XSI
echo \foo     implementation-defined         form-feed then "oo"           
echo -n foo   implementation-defined         "-n foo"
echo foo      "foo"                          "foo"
echo --       "--"                           "--"
echo -e foo   "-e foo"                       "-e foo"
echo -e \foo  implementation-defined         "-e " then form-feed then "oo"

each result would be followed by a newline.

I believe dash mimics the XSI column, whereas bash makes up it's own rules.  
Again, the shell command "printf" is much more consistent.
Boyd Stephen Smith Jr.           	 ,= ,-_-. =.
bss@iguanasuicide.net            	((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy 	 `-'(. .)`-'
http://iguanasuicide.net/        	     \_/

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply to: