Re: problems with loop in bash



Regarding echo vs. printf,

pk wrote:

Printf has another advantage over echo: it recognizes many special escape sequences that echo does not recognize (or recognizes only with -e in certain implementations), like \n or \t.


Houghi, pk and I represent different sides of the problem. I've worked most
of my UNIX life in environments where echo DID recognize \n, \t, etc. So I
expect other behaviors, such as pk considers normal, as abberant.

I'd re-write pk's statement to say that as printf is consistant in recognizing
the escape sequences like \n, \t, etc. while echo is not.


If you are going to do simple literal text that does not contain escape
sequences, go ahead and use echo. For example:

echo "Hello World!"

But is that easier than:

printf "Hello World!\n"

Well I guess it is harder because you have to add the '\n' in printf. But
that is balanced by times you don't want a newline. For example suppose you
want to ask for some data to be keyed in by the operator. Further, suppose
you want the data entry on the same line as the prompt. With printf
you might do this:

printf "Enter your last name: "
read LastName

But with echo you would have to remember, does my echo suppress the default
newline by putting a '\c' at the end or by using a '-n' option? I.e. should
it be

echo "Enter your last name: \c" OR echo -n "Enter your last name: "

And what about when I copy my script to my laptop, or my next new computer.
Will its echo work the same way as this version?

I realize you are not "smart enough" to learn the intricacies of printf :)
but I find the ability to format multiple lines of simple text a great
feature. For example, suppose you are printing a list of names, first and
last. You could use a printf like this:

printf "%s %s\n" "$FirstName" "$LastName"

with output like (which is no different than you could get with echo):

George Washington
John Adams
Thomas Jefferson

But changing each %s to %12s would change the output to:

George Washington
John Adams
Thomas Jefferson

Ahh, maybe you wanted left instead of right alignment, make it %-12s:

George Washington
John Adams
Thomas Jefferson

Or the first one %12s and the second one %-12s

George Washington
John Adams
Thomas Jefferson

Lots and lots of flexibility that would be difficult to do with echo.
Yet the simple things are still simple (and more portable) with printf.

Jon
.



Relevant Pages

  • Re: 2.6.33-rc1: LZMA kernel fails to decompress
    ... size_append command scripts/Makefile.lib. ... Specifying the full path forces to use the system echo command ... In 2.6.33, for some reason, echo has been replaced with printf. ... real solution, which is to stop doing all this hackery altogether, ...
    (Linux-Kernel)
  • Re: What improvements would you make to this script?
    ... Why are you so keen on the use of printf ... instead of echo. ... syntax (for doing things like not outputting a newline) is ugly. ...
    (comp.unix.shell)
  • Re: problems with loop in bash
    ... Using printf lets you do many things that echo can't do, ... echo "$i bottles of beer on the wall..." ...
    (comp.unix.shell)
  • Re: What improvements would you make to this script?
    ... Why are you so keen on the use of printf ... instead of echo. ... syntax (for doing things like not outputting a newline) is ugly. ...
    (comp.unix.shell)
  • Re: How do I print AT (particular coordinates)?
    ... Note that it is tput cup y x, and the origin is in the ... if you know you're always going to use it on terminals like the ... CSI sequences - almost everything these days talks at least vt220) then ... (Your echo or print needs to expand escape sequences. ...
    (comp.unix.shell)