Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the difference between the printf function in bash and the echo function?

Specifically, running:

echo "blah" >/dev/udp/localhost/8125

did not send the "blah" command to the server listening on 8125, whereas

printf "blah" >/dev/udp/localhost/8125

sent the data. Does printf send an extra EOF at the end of its output?

share|improve this question
2  
Given that it is UDP, did you make sure to repeat your above experiments several times? The only difference between echo "blah" and printf "blah" is echo will output an extra newline. Does that matter to the server? Does the order when receiving several commands in a row matter? EOF is not something you send or even a character - it's a condition you receive when there is nothing left to read. – jw013 Dec 12 '12 at 19:11
1  
How did you verify that the echo-attempt does not send the string to the server? The proper way would be to monitor lo and not rely on the server printing stuff. There is always iptables ... – user1129682 Dec 12 '12 at 21:44

migrated from serverfault.com Dec 12 '12 at 15:44

4 Answers

The difference is that echo sends a newline at the end of its output. There is no way to "send" an EOF.

share|improve this answer
3  
There are more differences than that. – jlliagre Dec 12 '12 at 16:05
1  
Although this answer does (lightly) explain why the first command and second command differ, which is really what Kevin is asking. – Stefan Lasiewski Dec 12 '12 at 18:19

Here is probably the best description of echo vs printf

http://www.in-ulm.de/~mascheck/various/echo+printf/

At a very high level.. printf is like echo but more formatting can be done.

share|improve this answer
1  
Good reference, but not really an answer to the question, nor an explanation why printf works and echo doesn't. – gertvdijk Dec 12 '12 at 16:39

For trivial messages (no escape sequences, no options), echo is fine. As soon as you need something more complex, you shoud use printf which is portable (i.e. behave the same regardless of the OS) and allows much better formatting, being designed from the same name C function.

Should you want to call echo in your example, the correct way would be either:

echo -n "blah" >/dev/udp/localhost/8125

or

echo "blah\c" >/dev/udp/localhost/8125

depending on the echo implementation you use.

share|improve this answer

echo prints its argument followed by a newline. With multiple arguments, they are separated by spaces. Depending on the unix variant, the shell and the shell options, it may also interpret some escape sequences beginning with \, and it may treat the first argument(s) as options if they start with -.

printf interprets its first argument as a format, and subsequent arguments as arguments to the % specifiers. No newline is added unless you specify one. In the first argument, all characters except two are interpreted literally: % starts a printf specifier, and \ starts an escape sequence (e.g. \n for a newline character).

Because different shells work differently, echo "$string" does not always print the specified string plus a newline. A reliable way of printing a string is printf %s "$string". If you want a newline after the string, write printf '%s\n' "$string".

In your case, assuming that blah doesn't start with a - or contain % or \, the only difference between the two commands is that echo adds a newline and printf doesn't.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.