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@xxxxxxxxxxxxxxxxx ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/

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



Relevant Pages

  • Re: [Fedora] Re: FC6 not connecting
    ... echo $PATH ... as user that performs 'su -' # note the dash ... Nobody goes near my machines but me; ... command on it, and stick to that. ...
    (Fedora)
  • Re: [semi-OT] How to echo \ in dash
    ... In an effort to write my scripts in a posix conform way, ... dash instead of bash. ... Per the POSIX and SUS specifications, the echo command, which maybe be built ...
    (Debian-User)
  • Re: Why cannot I find the Symbol dialog box in the Insert menu?
    ... Charles has told you how to restore the command; I will tell you how you ... You wanted to insert an em dash. ... ToolsCustomizeRemoveMenuShortcut is to remove any menu item you click on. ... Word MVP FAQ site: http://word.mvps.org ...
    (microsoft.public.word.docmanagement)
  • Re: Shell script one-liner
    ... when Ubuntu or Debian change the default non-interactive shell to dash, ... let alone bash (echo -e). ... I think Ubuntu have already done it, which is daft, AFAIK Debian are ...
    (uk.comp.os.linux)
  • Re: chapter: vertical spacing and access to the title
    ... First of all, I would like to decrease the vertical space, which is ... little more able to control the look of the \chaptercommand. ... i.e., an added space, a dash, and a symmetric space. ... I would change this to just a font command. ...
    (comp.text.tex)