From: Thomas 'PointedEars' Lahn on
Seebs wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Ben Finney wrote:
>>> gazelle(a)shell.xmission.com (Kenny McCormack) writes:
>>>> Replace the above with:
>>>>
>>>> echo "Hello World."
>>>
>>> Or, more portably:
>>>
>>> printf "Hello World.\n"
>>
>> How did you get that idea?
>
> In this particular case, there's no real difference. As a general rule,
> if you have a choice between echo(1) and printf(1), you should use
> printf(1) often enough that it's probably best to just always use it.

IBTD. I use printf(1) only when necessary, i.e. when I don't want a
trailing newline, or when I need a value to be formatted.

> Obviously, in this case, they're identical. However, if you have any
> variable expansion going on, it is quite easy for echo to blow up in
> inconvenient and/or surprising ways, where printf will be just fine.

-v please

> Add in the portability hassles induced by the whole -n\c thing, and
> the difficulty of sanitizing inputs enough to make sure that you aren't
> going to run afoul of some "helpful" extension...

Yes, don't use `echo -n', or `echo -e' for that matter. That's not a good
reason to always use printf(1), though.


PointedEars
From: Seebs on
On 2010-06-11, Thomas 'PointedEars' Lahn <PointedEars(a)web.de> wrote:
>> Obviously, in this case, they're identical. However, if you have any
>> variable expansion going on, it is quite easy for echo to blow up in
>> inconvenient and/or surprising ways, where printf will be just fine.

> -v please

?

>> Add in the portability hassles induced by the whole -n\c thing, and
>> the difficulty of sanitizing inputs enough to make sure that you aren't
>> going to run afoul of some "helpful" extension...

> Yes, don't use `echo -n', or `echo -e' for that matter. That's not a good
> reason to always use printf(1), though.

But there are some echos which will do surprising things with other "-x"
type arguments, at least a couple which "helpfully" interpret \ sequences
without any prompting, and so on...

And here's the thing. It's never *bad* to use printf(1) on anything newer
than, I think, SunOS 4.1. So if you just always use it, life is simpler
and better than if you try to figure out whether you need echo or printf,
and sometimes you guess wrong, and sometimes later revisions to the code
break an echo but would have been fine with a printf, and so on.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Thomas 'PointedEars' Lahn on
Seebs wrote:

> Thomas 'PointedEars' Lahn wrote:
>>> Obviously, in this case, they're identical. However, if you have any
>>> variable expansion going on, it is quite easy for echo to blow up in
>>> inconvenient and/or surprising ways, where printf will be just fine.
>> -v please
>
> ?

(How can you be posting here and not knowing about `-v'? ;-))

I mean, please be verbose. IOW, please provide an example that explains
your argument.

>>> Add in the portability hassles induced by the whole -n\c thing, and
>>> the difficulty of sanitizing inputs enough to make sure that you aren't
>>> going to run afoul of some "helpful" extension...
>> Yes, don't use `echo -n', or `echo -e' for that matter. That's not a
>> good reason to always use printf(1), though.
>
> But there are some echos which will do surprising things with other "-x"
> type arguments, at least a couple which "helpfully" interpret \ sequences
> without any prompting, and so on...

So do not use those options either as they are not portable. That is still
no reason to insist on printf(1) when not necessary.

> And here's the thing. It's never *bad* to use printf(1) on anything newer
> than, I think, SunOS 4.1. So if you just always use it, life is simpler
> and better

Simpler and better *for whom*?

> than if you try to figure out whether you need echo or printf, and
> sometimes you guess wrong,

How could I guess wrong if I don't use any options?

> and sometimes later revisions to the code break an echo but would have
> been fine with a printf, and so on.

printf(1) needs to scan the entire string argument for formatting strings
and escape sequences; echo(1) without arguments does not.


PointedEars
From: Kenny McCormack on
In article <slrni14h94.iv2.usenet-nospam(a)guild.seebs.net>,
Seebs <usenet-nospam(a)seebs.net> wrote:
....
>And here's the thing. It's never *bad* to use printf(1) on anything newer
>than, I think, SunOS 4.1. So if you just always use it, life is simpler
>and better than if you try to figure out whether you need echo or printf,
>and sometimes you guess wrong, and sometimes later revisions to the code
>break an echo but would have been fine with a printf, and so on.

Like a stopped clock, and against all odds, Mr. Seebs is actually right
here. I realized quite a while ago that it was easier and better to
just always use printf.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

From: Seebs on
On 2010-06-11, Thomas 'PointedEars' Lahn <PointedEars(a)web.de> wrote:
> Seebs wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>>> Obviously, in this case, they're identical. However, if you have any
>>>> variable expansion going on, it is quite easy for echo to blow up in
>>>> inconvenient and/or surprising ways, where printf will be just fine.
>>> -v please

> (How can you be posting here and not knowing about `-v'? ;-))

I know about it, but not about that usage.

> I mean, please be verbose. IOW, please provide an example that explains
> your argument.

echo $foo is a variable.

What does this do?

What if someone had executed:
foo="-n"

What if someone had executed:
foo='\'

What about:
foo='-e'

What about:
foo='\c'

The problem is, echo can easily blow up on some systems (but not on others!)
for a broad variety of inputs, and for all we know, there's more to come.

Imagine:
for i in $known_opts
do
eval description=description_$i
echo "--$i" " $description"
end

Now what happens when you hit a version of echo which "helpfully" accepts
some of $known_opts as extensions?

>> But there are some echos which will do surprising things with other "-x"
>> type arguments, at least a couple which "helpfully" interpret \ sequences
>> without any prompting, and so on...

> So do not use those options either as they are not portable.

You don't seem to be comprehending.

The problem is not intentionally using them. The problem is expanding
values which, being runtime values, you *did not know in advance*, and yet,
which turn out to accidentally trip those options.

> That is still
> no reason to insist on printf(1) when not necessary.

It's not an insistance, it's a piece of advice about good style and
effective defensive coding.

If you use printf to display output, you have one less point of failure
when trying a new machine or running with user input.

>> And here's the thing. It's never *bad* to use printf(1) on anything newer
>> than, I think, SunOS 4.1. So if you just always use it, life is simpler
>> and better

> Simpler and better *for whom*?

The scripter.

>> than if you try to figure out whether you need echo or printf, and
>> sometimes you guess wrong,

> How could I guess wrong if I don't use any options?

That's brilliant! I suppose your solution to SQL injection attacks
is that you don't do any special quoting, and you just don't actually
write "DROP TABLE USERS;" in any of your SQL.

The problem, again, is not intentionally using these features, but that
you have to know what every possible innovative version of echo will do,
and go to great lengths to ensure that nothing you ever pass to echo will
trip any of these.

> printf(1) needs to scan the entire string argument for formatting strings
> and escape sequences; echo(1) without arguments does not.

This is why:
printf "%s\n" "$var"
is the ideal idiom. You can be confident that NOTHING in $var will
result in anything but the plain literal text being output.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!