What `echo` does in Linux, what `echo -n` is, and `echo` vs newlines (`\n`)

What echo does on Linux, the echo -n flag (no trailing newline), why echo n is not echo -n, how echo -e handles backslash escapes like \n, and when to use printf instead.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

What `echo` does in Linux, what `echo -n` is, and `echo` vs newlines (`\n`)

What does echo do on Linux? The echo builtin writes its arguments to stdout, separated by spaces, then prints a trailing newline unless you turn that off. The -n flag means “do not append that final newline,” which is why prompts and progress text often use bash "echo -n" so the cursor stays on the same line.

Output below was produced with Bash 5.2.37 on Ubuntu 25.04 (kernel 6.14.0-37-generic).


Default echo: one line, then a newline

bash
echo "This is an echo example"
text
This is an echo example

The shell prints the text, then drops to the next line because of the newline echo adds by default.


echo -n: same text, no trailing newline

bash
printf '>'; echo -n "This is an echo example"; printf '<\n'
text
>This is an echo example<

Markers > / < show there is no newline between the sentence and the following prompt—exactly the echo -n behavior people look up for Y/N prompts and status fragments.


echo n, bash echo n, and echo "-n" (easy mistakes)

  • echo n prints the literal letter n (one argument). It is not the same as echo -n (a flag plus more arguments).
  • echo "-n" hello is a sharp edge: on GNU bash, the first argument "-n" is still treated like the -n option, so you may get hello with no newline—not the text -n. To print a literal -n, use printf '%s\n' -- '-n' (or another form that never treats the first column as an echo flag).

echo \n vs echo -e and real newlines

Single quotes keep backslashes literal; -e turns escapes on for bash echo:

bash
echo 'foo\nbar'
echo -e 'foo\nbar'
text
foo\nbar
foo
bar

So bash echo \n searches often mix two ideas: a literal \n two-character sequence versus an escaped newline, which needs -e (or $'…' ANSI-C quoting: echo $'foo\nbar').


Small script: prompt on one line

bash
#!/usr/bin/env bash
read -r -p "Do you want to quit? [Y]/N: " input
case ${input:-Y} in
  [yY]) echo -n "Y" ;;
  [nN]) echo -n "N" ;;
  *)    echo "[Y]/N" ;;
esac
echo

read -p already keeps the question on one line; echo -n here only shows how you might echo a choice without an extra blank line before the next command.


Prefer printf in portable scripts

echo flags and escape rules differ between bash, dash, and BSD /bin/echo. For anything non-toy, printf is predictable:

bash
printf 'Name: '
printf '%s\n' "$USER"

See help echo / help printf in bash, or man 1 echo on your distro.



Summary

What does echo do in linux: print arguments with spaces and a default trailing newline. What is echo -n: skip that newline so the next output shares the line—classic bash echo -n prompt trick. echo n is just printing n; echo "-n" can still be parsed as an option on GNU bash, so use printf when the data might start with -. For real newlines inside a string, use echo -e '...\n...', $'…', or printf—not a bare echo \n unless you understand how your shell quotes the backslash.

Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …