0

I'm not quite versed on the terminal, but the few things I've known and read lead me to believe that printf is much more powerful and confortable than echo but work for similar (if not same) purposes. So I asked myself, is it sensible to alias echo to printf so I can forget about any issues with echo? Or is there something I don't see like portability or else?

If needed, system is Ubuntu 20.04 running on WSL2 on Windows 10.

1 Answers1

4

No, it is not sensible to do so.

printf works very different to echo, you should rather change your habits and use printf when possible (always, but for easy tasks more complicated and more to think about) and/or beneficial/necessary (when variables involved, repetitions, special chars, formating of numbers, etc.).


Just one simple example:

$ echo "100%"
100%

$ printf "100%" bash: printf: `%': missing format character

Another example:

$ echo "Hello" "World"
Hello World

$ printf "Hello" "World" Hello

Note also, for printf, that after Hello there is no newline.

This is, because the first argument to printf is the FORMAT string:

printf FORMAT [ARGUMENT]...

See also man printf.

pLumo
  • 22,565
  • Thanks for your answer. I highly appreciate your time and kindness in it.

    But it prompts a doubt on me, isn't the appropriate response to the first difference changing double quotes for single?

    And I will check that man, although I'm afraid I might not understand all of it

    – tms8bltrn Feb 14 '22 at 14:20
  • Double or single quotes make no difference in the first example, you could even leave them out and have the same effect (same for the second example btw). % is not special to bash or echo, but to printf – pLumo Feb 14 '22 at 14:20