echo vs printf
1 min read

echo vs printf

is echo letting you down? printf may help.
echo vs printf

today I learned that echo appends a \n to its input.

If you're doing something like this :

echo "y" | <some command that prompts [y/N]>

You're probably fine with echo. However, if you're doing something like this :

echo "${some_var}" | <command expecting var from stdin>

Here is where the issues begin. Not even echo -n could remove the trailing newline (If you know why, please let me know).

I fixed this by swapping echo with printf. This cost me an hour of dev time. From now on I will be using printf over echo by default.