90

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.

all 4 comments
sorted by: hot top controversial new old
[-] kubica@fedia.io 30 points 1 month 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.

[-] clay_pidgin@sh.itjust.works 7 points 1 month ago

That seems like a good tip.

[-] boredsquirrel@slrpnk.net 12 points 1 month ago

Literally dealt with this just yesterday

I guess I need to change quite some things

this post was submitted on 31 Dec 2025
90 points (96.9% liked)

Linux

11584 readers
419 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS