20

For displaying message and print data there are two command available printf and echo.
Then how differently they used? Which is more preferable?

Avinash Raj
  • 77,204
  • 56
  • 214
  • 254
Pandya
  • 34,843
  • 42
  • 126
  • 186

2 Answers2

33

Preferable and most widely used is not the same thing. While printf is better for many reasons, most people still use echo because the syntax is simpler.

The main reasons why you should prefer printf are:

  1. echo is not standardized, it will behave differently on different systems.
  2. It is hard to predict what you're actually running when you echo foo. To illustrate, on my Debian system:

    $ type -a echo
    echo is a shell builtin
    echo is /bin/echo
    

    As you can see, there are two different echo commands, one is a shell (bash in this case) builtin and another is a separate binary. Note that bash also has a printf builtin but its behavior is more standardized so it is less of an issue (thanks to @ RaduRădeanu for pointing it out).

  3. Since some (but not all) implementations of echo support command line switches, it is hard to print a string that starts with a -. While many programs support -- to signify the end of switches and the beginning of arguments (for example, grep -- -a file will find lines in file that contain -a), echo does not. So, how do you have echo print -n?

    $ echo -n           ## no output
    $ echo '-n'         ## no output
    $ echo "-n"         ## no output
    $ echo \-n          ## no output
    $ echo -e '\055n'   ## using the ASCII code works but only on implementations
    -n                  ## that support -e
    

    printf can do this easily:

    $ printf -- '-n\n'
    -n
    $ printf '%s\n' -n
    -n
    $ printf '\055n\n' 
    -n
    

For more information than you ever wanted to know on why printf is better than echo, see this answer to a similar question on http://unix.stackexchange.com:

https://unix.stackexchange.com/a/65819/22222

terdon
  • 98,183
  • 15
  • 197
  • 293
  • 1
    Given good answer. It helps me to prefer `printf` as better interpreter. – Pandya May 16 '14 at 13:45
  • 3
    At the point 2: the same is true for `printf` (see `type -a`). – Radu Rădeanu May 16 '14 at 16:31
  • 2
    @RaduRădeanu true, but `printf` is more rigidly [defined](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html) in POSIX and I think the builtin follows the same standard. The POSIX [specs](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) for `echo` on the other hand are less strict and even explicitly state that there will be differences between implementations and that `printf` should be preferred. – terdon May 16 '14 at 16:36
  • Wow. So there are **literally zero objective technical advantages to using `echo`.** Is that correct? – JamesTheAwesomeDude May 01 '16 at 23:47
  • 1
    @JamesTheAwesomeDude no, absolutely no technical advantage whatsoever. The only reason to use `echo` rather than `printf` is that the forma can be simpler. If you know what you're printing, `echo foo` is faster and easier to type than `printf 'foo\n'`. Apart from that, when writing scripts where a variable needs to be printed, there's never a good reason to use `echo`. – terdon May 02 '16 at 10:03
3

To ask which is preferable is itself incomplete.

If all one wishes to do is emit one or more lines of text terminated by newlines, then echo suffices. If anything more clever is intended, specifically including a "partial line" that does not have the newline, then printf is best for that purpose.

  • Depending on the `echo` implementation, "avoiding the newline" is easily done with `echo -n`. In fact, `-n` is the most portable of echo's features. According to POSIX, it is absent from SystemV but present in most Unices. – terdon May 16 '14 at 19:25
  • I've worked in enough different unices to not even try avoiding newlines with echo. Writing scripts that are portable across SCO, AIX, HP-UX, and Linux is... challenging. – Monty Harder May 16 '14 at 19:48
  • Oh absolutely, no argument there. All I'm, saying is that the newline is the least of the problems. All bets are off as soon as you try to port to a non-GNU system. I _think_ you can rely on the `-n` for the BSDs and SystemV but anything else, I have no idea. – terdon May 16 '14 at 21:29