logo
search
list

Table of Content

Basic Syntax
Useful Options (-n, -e, -E)
Variables and Quoting
Write (or Append) to a File
Newlines and Command Substitution (Classic Gotcha)
Why /bin/echo Exists
Echo vs Printf
Colors and Escape Sequences (Optional)
Common Mistakes
Quick Cheat Sheet
From Shell Output to Shareable Documents
FAQ

Echo Command in Linux: Print Text & Escape Sequences (Ubuntu)

Posted by Algirdas Jasaitis

calendar

2026-07-27

views

880

likes

5

The echo command in Linux writes its arguments to standard output, usually followed by a newline. On Ubuntu you almost always use the shell builtin (type echoecho is a shell builtin). GNU coreutils also ships /bin/echo for cases where no shell is involved. Official note from man echo: your shell’s echo may differ; printf is preferred when portability or option-like strings matter.

type -a echo
# echo is a shell builtin
# echo is /bin/echo

help echo          # bash builtin help
man echo           # documents /bin/echo (coreutils)

If you already know your goal, jump to: -n/-e, write to a file, newline quoting, or echo vs printf. To view files afterward, use cat or tail.

Concept illustration of the Linux echo command printing Hello World and multi-line text in a terminal
Echo writes text to standard output—use -n to skip the newline, -e for escapes like \n, and prefer printf in portable scripts.

For a broader Ubuntu terminal quick reference, see the Linux commands cheat sheet.

Basic Syntax

echo [OPTION]... [STRING]...
echo Hello
echo Hello World
echo "Hello World"

Default behavior: print the strings separated by spaces, then a trailing newline.

Useful Options (-n, -e, -E)

GNU /bin/echo (and bash’s builtin with similar flags) supports:

OptionMeaning
-nDo not print the trailing newline
-eEnable backslash escapes (\n, \t, …)
-EDisable escapes (default for /bin/echo)
echo -n "Prompt: "          # cursor stays on same line
echo -e "Line1\nLine2"      # two lines
echo -e "Col1\tCol2"        # tab between columns
echo -e "Path: C:\\\\temp"  # show a backslash

Common escapes with -e (from the Ubuntu man page):

EscapeMeaning
\nNewline
\tTab
\\Backslash
\aBell
\bBackspace
\rCarriage return
\cStop output here
\xHHHex byte
\0NNNOctal byte

Bash builtin details: help echo. External binary: man echo.

Variables and Quoting

name=Ubuntu
echo $name
echo "$name"              # safer with spaces/glob chars
echo "User: $USER home=$HOME"
echo 'Literally $USER'    # single quotes: no expansion

Unquoted $var still undergoes word-splitting and globbing—prefer "$var" in scripts.

Write (or Append) to a File

echo "first line" > notes.txt       # overwrite
echo "second line" >> notes.txt     # append

Root-owned files: sudo echo … > /etc/file fails—the shell opens the file as you. Use:

echo "options ..." | sudo tee /etc/modprobe.d/local.conf >/dev/null
echo "line" | sudo tee -a /etc/file >/dev/null

(Same Ask Ubuntu pattern as other sudo + redirection issues—see the sudo command guide.)

Newlines and Command Substitution (Classic Gotcha)

# File has multiple lines, but this prints one line:
echo $(cat input.txt)
# hello world !

# Keep newlines — quote the substitution:
echo "$(cat input.txt)"

Ask Ubuntu explanation: without quotes, word-splitting turns newlines into spaces. Also, $(…) strips trailing newlines—use cat/printf carefully when byte-exact output matters.

Why /bin/echo Exists

Ask Ubuntu: the builtin is for speed; /bin/echo exists because tools like find -exec, xargs, .desktop Exec=, and sudo echo run programs without a shell:

find src -name 'abc*' -type f -exec echo mv -nv {} dest/ \;
type -a echo
/bin/echo --version    # coreutils version string

Prefer printf over depending on differences between bash echo and /bin/echo.

Echo vs Printf

UsePrefer
Quick interactive printecho
Scripts, formats, leading - textprintf
Portable escape handlingprintf
# echo can mis-handle strings that look like options:
echo -n               # may mean “no newline,” not print “-n”

# printf is explicit:
printf '%s\n' -n
printf 'Name: %s\n' "$USER"
printf 'Line1\nLine2\n'

The coreutils man page explicitly recommends printf(1) as the preferred alternative.

Colors and Escape Sequences (Optional)

echo -e "\033[1;32mOK\033[0m"
# or
printf '\033[1;31mError\033[0m\n'

Works in terminals that understand ANSI codes; avoid relying on color in logs meant for files.

Common Mistakes

MistakeFix
Lost newlines with echo $(cat f)echo "$(cat f)" or just cat f
sudo echo x > /root/f permission deniedecho x | sudo tee /root/f
Expecting -e everywhereBehavior varies by shell; use printf in scripts
Unquoted variablesUse "$var"
Need exact bytes / formatsPrefer printf

Quick Cheat Sheet

echo hello
echo -n "no newline"
echo -e "a\tb\nc"
echo "$VAR"
echo "line" > file
echo "line" >> file
echo x | sudo tee /path >/dev/null
printf '%s\n' "$VAR"          # safer in scripts
type -a echo

From Shell Output to Shareable Documents

echo is a fast way to print values, append small lines, or build simple shell output. But when that output becomes a note, checklist, or attachment for someone else, plain terminal text is only the first step.

WPS Office for Linux gives Ubuntu a cleaner desktop handoff: Writer for formatted notes, Spreadsheet for structured values, and PDF for fixed output.

WPS Office Writer on a Linux desktop editing an incident report DOCX with a familiar ribbon toolbar
WPS Writer on Linux: turn setup notes, terminal findings, and troubleshooting steps into a DOCX or PDF that teammates can open easily.
What you need nextWhat WPS gives you
Turn shell output into a readable final documentStrong .docx / .xlsx / .pptx layout fidelity when files move between Linux, Windows, and macOS
One place for notes, sheets, slides, and PDFsFree core suite: Writer, Spreadsheet, Presentation, plus PDF view/edit/convert tools
A familiar desktop UI for switchersRibbon-style apps with a lighter footprint than many heavy suites
A trusted install pathOfficial DEB (Ubuntu/Debian/Mint) and RPM packages from the vendor

Use the official WPS Office for Linux download page, then install the local package with sudo apt install ./wps-office_VERSION_amd64.deb. Keep echo for shell text; use WPS when the result should open as a polished file instead of another terminal line.

FAQ

What does the echo command do in Linux?
It prints its arguments to standard output, normally ending with a newline.

What is echo -e?
It enables interpretation of backslash escapes like \n and \t (GNU echo / typical bash builtin).

What is echo -n?
It suppresses the trailing newline—useful for prompts.

Builtin vs /bin/echo?
Interactive bash uses the builtin; /bin/echo is for non-shell callers (find -exec, xargs, etc.).

When should I use printf instead?
In scripts, for reliable formatting, and when the string might look like an option.

Where is the official documentation?
Run help echo (bash), man echo (/bin/echo), and man printf, or see the Ubuntu echo(1) man page.

Algirdas Jasaitis

15 years of office industry experience, tech lover and copywriter. Follow me for product reviews, comparisons, and recommendations for new apps and software.