Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Why is printf better than echo?

I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 but printf did. But apparently, there are other differences, and I would like to inquire what they are as well as if there are specific cases when to use one vs the other.
by

1 Answer

Kajalsi45d
You might want to use printf for its formatting options. echo is useful when it comes to printing the value of a variable or a (simple) line, but that's all there is to it. printf can basically do what the C version of it can do.

Example usage and capabilities:
Echo:
echo "
Backup shell script
"
echo
echo "Runtime: $(date) @ $(hostname)"
echo


printf:
vech="bike"
printf "%s\n" "$vech"

Login / Signup to Answer the Question.