Unix Shell - bash question ... echo subtleties

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > bash question ... echo subtleties





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash question ... echo subtleties
Yakov

2007-12-08, 7:42 pm

Consider (echo "$X" >$file) [this is bash].

What if $X is "-n" ? That would break the code.

What is good way to make (echo "$X" >>$file) work in all cases ?

Y.L.
Joachim Schmitz

2007-12-08, 7:42 pm

"Yakov" <iler.ml@gmail.com> schrieb im Newsbeitrag
news:97f76b73-4355-48c3-bb0b-7054001d4c8c@w28g2000hsf.googlegroups.com...
> Consider (echo "$X" >$file) [this is bash].
>
> What if $X is "-n" ? That would break the code.
>
> What is good way to make (echo "$X" >>$file) work in all cases ?

Use (printf "%s\n" "$X" >>"$file")

Bye, Jojo


Cyrus Kriticos

2007-12-08, 7:42 pm

Yakov wrote:
> Consider (echo "$X" >$file) [this is bash].
>
> What if $X is "-n" ? That would break the code.
>
> What is good way to make (echo "$X" >>$file) work in all cases ?


printf "%s\n" "$X" >$file

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
mallin.shetland

2007-12-08, 7:42 pm

Yakov scrisse:

> Consider (echo "$X" >$file) [this is bash].
>
> What if $X is "-n" ?


This is a dirty workaround echoing simple '-n'
with bash:

echo -e '-/x0e'

If it is not too hard you can raise one level and
modify code setting X or use bash prinf function.


Mark Hobley

2007-12-08, 7:42 pm

mallin.shetland <mallin.shetland@aol.com> wrote:
>
> echo -e '-/x0e'
>


Out of interest, I tried the -- parameter, which conventionally means that all
arguments that follow are not parameters, on both the inbuilt command, and the
external binary:

echo -- -n
-- -n

/bin/echo -- -n
-- -n

Unfortunately, this didn't quite work, because -- echos to the screen. I'll
leave you to decide whether that is a bug or a feature (I say bug).

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Stephane Chazelas

2007-12-08, 7:42 pm

On Sat, 08 Dec 2007 15:08:03 GMT, Mark Hobley wrote:
> mallin.shetland <mallin.shetland@aol.com> wrote:
>
> Out of interest, I tried the -- parameter, which conventionally means that all
> arguments that follow are not parameters, on both the inbuilt command, and the
> external binary:
>
> echo -- -n
> -- -n
>
> /bin/echo -- -n
> -- -n
>
> Unfortunately, this didn't quite work, because -- echos to the screen. I'll
> leave you to decide whether that is a bug or a feature (I say bug).

[...]

That's not a bug, that's how it's specified. And that's one of
the many reason it is strongly advised not to use echo, even by
the POSIX, Unix specification itself.

The POSIX specification itself states that echo can't be relied
upon, unless its arguments don't start with "-n" and doesn't
contain any slash, and one should use printf instead.

In practice, it's even worse than that, as some echo
implementations allow other options than -n (see for instance
bash or zsh that support -e).

Note that zsh supports:

echo -E - $string

as un alternative to

printf '%s\n' "$string"

zsh also being a ksh interpreter, it supports

print -r -- "$string"

like ksh.

--
Stephane
mallin.shetland

2007-12-09, 1:25 pm

Mark Hobley scrisse:

> mallin.shetland <mallin.shetland@aol.com> wrote:
>
> Out of interest, I tried the -- parameter, which..



.... does not work.

It does not work with every command.
It does not work with each version of a command.
It does not work in all *nix or all shell.
It does not work with echo.

By the way this is



Mark Hobley scrisse:

> ... conventionally means that all arguments...


Unfortunately everyone is free to choose conventions
he prefers.
I like a '-' states 'end of argument', GNU prefers '--',
echo ignore both these conventions.




Mark Hobley scrisse:

> I'll leave you to decide whether that is a bug or a feature (I say bug).


I think it is a POSIX feature. :P
No I *REALLY* think both 'echo -e ...' and 'echo -n ...' are
great mistakes but it is too late to do anything.

.... or anything but use printf instead of echo.


mallin.shetland

2007-12-09, 7:21 pm

Yakov scrisse:

> What if $X is "-n" ? That would break the code.


Kernigham & Pike give this unelegant solution:

echo -n "$X
" >> $file

Stephane Chazelas

2007-12-09, 7:21 pm

On Sun, 09 Dec 2007 19:45:30 +0100, mallin.shetland wrote:
[...]
> I like a '-' states 'end of argument', GNU prefers '--',
> echo ignore both these conventions.


GNU and POSIX and every Unix. "-" states the "end of arguments"
only in a few utilities (including sh where "--" is supported as
well).

"-" is more often used to refer to stdin or stdout when used in
place of a file argument used for input or output respectively
(which is a pain as it means you need to take that into account
as in):

case $file1 in
(-*) file1=./$file1;;
esac
case $file2 in
(-*) file2=./$file2;;
esac
paste "$file1" "$file2"

> Mark Hobley scrisse:
>
>
> I think it is a POSIX feature. :P
> No I *REALLY* think both 'echo -e ...' and 'echo -n ...' are
> great mistakes but it is too late to do anything.

[...]

No, POSIX allows "-n" and says the behavior is unspecified if
it's provided. This is so that BSD shells are still POSIX
conformant, but of course that means you shouldn't use echo -n
in POSIX scripts. Other options like "-e" are not allowed by
POSIX and in that bash is not POSIX conformant. This is
intentional and claimed by GNU. "echo -n" is in the GNU standard
and you'll find that GNU doesn't discourage its usage.

Note that the Unix spec (SusV3 with XSI option) goes further in
that no option are supported (echo -n should output "-n<LF>")
but the Unix echo is also meant to expand the \n, \t, \f...
sequences, so that it can't be used reliably to output arbitrary
strings either.

--
Stephane
mallin.shetland

2007-12-09, 7:21 pm

Stephane Chazelas scrisse:

>
> GNU and POSIX and every Unix. "-" states the "end of arguments"
> only in a few utilities (including sh where "--" is supported as
> well).
> ...


Tanks for this correction.


Stephane Chazelas scrisse:

> ...
> "-" is more often used to refer to stdin or stdout...


I knew this but I forgotten saying it. Tanks.



> On Sun, 09 Dec 2007 19:45:30 +0100, mallin.shetland wrote:
> [...]
> No I *REALLY* think both 'echo -e ...' and 'echo -n ...' are
> great mistakes but it is too late to do anything.
> [...]


I would say I dislike both 'echo -e' and 'echo -n'.
This is my personal opinion and I am aware it counts for
nothing or even less.
I think echo should do one simple thing, echoing its
arguments; echo should not recognize backslash escapes,
echo should not admit options.

I don't want criticize POSIX standard or GNU choices,
I have not enough knowledge doing that and I want not
a flame war.




Stephane Chazelas scrisse:

> ...
> Note that the Unix spec (SusV3 with XSI option) goes further in
> that no option are supported (echo -n should output "-n<LF>")
> but the Unix echo is also meant to expand the \n, \t, \f...
> sequences, so that it can't be used reliably to output arbitrary
> strings either.


It should be clear I dislike this too;)
By the way old System V echo supported no option but expand a
special \c escape at end of line to suppress normal linefeed
at end of input.




PS I'm sorry for some typo (as 'Kernigham') and for some line
I forget to erase.

I hope my english grammar is quite correct; excuse me if this is not.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com