One of the first commands new Linux users learn is echo. Need to print some text or debug a script? Add an echo statement. However, from my usage, I’ve found printf to be far more powerful. Once you understand its basics, it becomes a safer default for printing output in scripts.

Why echo isn’t as simple as it looks

One of the biggest issues with echo is that its behavior isn’t fully standardized. Different shells handle it differently, especially when it comes to options and escape sequences. For example, whether echo -n actually suppresses the trailing newline or just prints it verbatim depends on the shell. The same goes for escape characters like \n or \t. They may work in one environment and be printed literally in another.

  • kubica@fedia.io
    link
    fedilink
    arrow-up
    27
    ·
    2 days ago

    I liked this part:

    As a rule of thumb, use echo for quick, interactive output and one-off checks. Use printf in scripts, logs, and anywhere output format matters. If you ever find yourself relying on echo options or escape sequences, that’s usually a sign that printf is the better tool.