DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Using echo in Linux: Quoting, Redirection, and When to Use printf

echo is handy for printing a quick message or checking a variable, but its behavior can be less predictable when you rely on options or backslash escapes. Knowing where the shell fits in—and when to switch to printf—helps avoid confusing output in scripts.

The shell processes text before echo sees it

A basic command prints its arguments to standard output, usually your terminal, followed by a newline:

echo Hello, Linux
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Linux
Enter fullscreen mode Exit fullscreen mode

The shell handles quoting and variable expansion before it runs echo. That means echo displays the value of a variable; it doesn't look up the variable itself.

echo "$HOME"
echo '$HOME'
Enter fullscreen mode Exit fullscreen mode

The first command prints the value of HOME. Double quotes allow the shell to expand the variable while keeping its value together as one argument, including if it contains spaces. Single quotes prevent expansion, so the second command prints the literal text $HOME.

Double quotes also allow command substitution. The shell runs date, substitutes its output, and passes the resulting text to echo:

echo "Today is $(date)"
Enter fullscreen mode Exit fullscreen mode

For more on making shell variables available to commands launched from the shell, see this guide to exporting environment variables in Linux.

Redirect output to a file

The shell—not echo—handles redirection. Use > to create a file or replace its contents, and >> to append to it:

echo "First line" > notes.txt
echo "Another line" >> notes.txt
Enter fullscreen mode Exit fullscreen mode

Be careful with >: if notes.txt already exists, its contents are overwritten. The second command adds a line at the end instead. If you're choosing between echo and other ways to create a file in Linux, keep that overwrite behavior in mind.

What do -n and -e do?

Two options you may see are -n and -e:

  • -n suppresses the final newline in common implementations.
  • -e interprets selected backslash escapes in implementations that support it. For example, \n represents a newline and \t a tab.

In Bash, this prints two lines:

echo -e "one\ntwo"
Enter fullscreen mode Exit fullscreen mode

However, echo can be a shell builtin or an external program, and implementations don't handle these options and escapes consistently. A command that works in one shell may behave differently in another. Avoid depending on -e for scripts that need predictable output.

You can inspect how your current shell resolves echo with this Bash command:

type -a echo
Enter fullscreen mode Exit fullscreen mode

It can show whether echo is a builtin and whether an executable with the same name is also available. The behavior you get depends on which implementation is used.

Use printf when the output needs to be predictable

For short messages, echo is usually enough. Prefer printf when you need formatted output, need precise control over newlines, or want predictable handling of backslashes across shells.

printf 'one\ntwo\n'
Enter fullscreen mode Exit fullscreen mode

For variables, use format placeholders such as %s and pass the values as separate, quoted arguments:

printf 'User: %s\nHome: %s\n' "$USER" "$HOME"
Enter fullscreen mode Exit fullscreen mode

Here, \n in the format string creates a newline, and each %s is replaced by the corresponding value. Quoting the variable arguments keeps their contents as data rather than letting spaces split them into multiple arguments.

This distinction matters when text might begin with a hyphen. With echo, option-like text can be ambiguous because some implementations interpret options. printf makes the format explicit:

printf '%s\n' '-starting with a hyphen'
Enter fullscreen mode Exit fullscreen mode

A quick rule of thumb

Use echo for a simple message when its default trailing newline is what you want. Use printf for formatted output or scripts where exact behavior matters. And remember that variable expansion, quoting, and redirection are handled by the shell before the command runs.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)