
Why is printf better than echo? - iso8859-1
http://unix.stackexchange.com/q/65803/14305
======
jfb
God, I hate posix. The UNIX Haters were right about a lot of things, but none
more so than the utter brain-damage of the shell's argument parsing unfeature.

~~~
D9u
Your angst is misdirected, unless you can specifically illustrate how the
current implementations of _echo_ are POSIX compliant.

The _printf_ command means "print, formatted," where "format" refers to
"positional parameters," and this functionality is not present in _echo_.

Signed, a Unix aficionado.

~~~
segmondy
they person asking this question must not know why (format ) is greater than
printf.

<http://www.gigamonkeys.com/book/a-few-format-recipes.html>

------
emillon
> On those echo that support options, there's generally no support of a -- to
> mark the end of options (zsh and possibly others support - for that though),
> so for instance, it's difficult to output "-n" in many shells.

It's good to know (while the key takeaway lesson is "use printf"):

    
    
        # bash
        $ echo -n
        $ echo -- -n
        -- -n
        $ echo - -n
        - -n
    
        # zsh
        $ echo -n
        $ echo -- -n
        -- -n
        $ echo - -n
        -n

~~~
dexen
Two nasty hacks:

    
    
      echo -e '\x2d\x6e'
      echo '' -n

~~~
comex
Note that the second one outputs " -n", which is probably not what you want.

------
mzs
I've seen really old scripts that use awk oneliners doing a printf (like echo
| awk '{ printf ...) cause (and I have to guess here) it was not built in to
/bin/sh and there was no /usr/bin/printf command either!

------
dxbydt
aside: for ~10 years (1995-2005) there wasn't a printf in java! There were
numerous printf libraries for sale( the most popular one was
<http://sharkysoft.com/software/java/lava3/printf/>), but jdk itself didn't
have a built-in printf until JDK 1.5, when it was introduced via
java.util.Formatter (
<http://www.docjar.com/html/api/java/io/PrintStream.java.html>)

As a former Sun consultant, the most common question I had to fend from
customers during those 10 years was the lack of a printf. Fortunately, Odersky
has much better taste & Scala shipped with a printf on day 1 in Predef.scala,
via
Console([https://github.com/scala/scala/blob/v2.10.1/src/library/scal...](https://github.com/scala/scala/blob/v2.10.1/src/library/scala/Console.scala)
)

