9 ms·
Why is printf better than echo?
- emillon 14y ago> 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
- jfb 14y agoGod, 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 14y agoYour 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 14y agothey person asking this question must not know why (format ) is greater than printf. http://www.gigamonkeys.com/book/a-few-format-recipes.html http://www.gigamonkeys.com/book/a-few-format-recipes.html
- Shish2k 14y agoI agree that there are a lot of gotchas, but how would you have a system that's anywhere near this powerful without them?
- winter_blue 14y ago> but how would you have a system that's anywhere near this powerful without them? The arcane UX that POSIX gives us has no connection or correlation whatsoever with the power of the system. For instance, an enhanced IPython shell alone would be 10x better than bash. You wouldn't loose any power in this manner. In fact, I think it would be good if thought about or reflected on what exactly "power" is. IMO, it's programmability / script-ability. And Python (again, imo) is a much better language than bash -- hence a shell based on it would deliver a far better UX than bash ever would.
- pjmlp 14y agoYou mean similar to what Smalltalk Workstations or Lisp Machines used to offer?
- ajross 14y agoAnd yet... serious Python hackers aren't using an enhanced IPython shell. Basically no one is using anything but bash, and even the contrarians aren't going any farther than zsh. This despite the fact that very few people who use these shells ever both to write scripts in them. Certainly people could learn "better" interactive shells. Yet they don't. Clearly the Bourne shell and its descendents must have been doing something right.
- minopret 14y agoThe alternative is, what, scsh?
- mzs 14y agoI'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 14y agoaside: 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/ 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 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/scala/Console.scala https://github.com/scala/scala/blob/v2.10.1/src/library/scal... )