Mastering the Linux echo Command: Syntax, Options, and Practical Examples
This guide explains the Linux echo command’s syntax, demonstrates how to use its various options such as -e, -n, and escape sequences, and provides step‑by‑step examples for printing text, variables, and redirecting output to files.
Introduction
The echo command is one of the most frequently used built‑in commands in Linux Bash and C shell, primarily for printing text to standard output.
Syntax
echo [option(s)] [string(s)]Common Usage Examples
1. Print a line of text
$ echo rumenz is a community of Linux NerdsOutput:
rumenz is a community of Linux Nerds2. Declare a variable and print its value
$ x=10 $ echo The value of variable x = $xOutput:
The value of variable x = 103. Use -e with \b (backspace) to remove spaces
$ echo -e "rumenz \bis \ba \bcommunity \bof \bLinux \bNerds" rumenzisacommunityofLinuxNerds4. Use -e with \n for new lines
$ echo -e "rumenz
is
a
community
of
Linux
Nerds" rumenz
is
a
community
of
Linux
Nerds5. Use -e with \t for horizontal tabs
$ echo -e "rumenz \tis \ta \tcommunity \tof \tLinux \tNerds" rumenz is a community of Linux Nerds6. Combine \n and \t
$ echo -e "
\trumenz
\tis
\ta
\tcommunity
\tof
\tLinux
\tNerds" rumenz
is
a
community
of
Linux
Nerds7. Use -e with \v (vertical tab)
$ echo -e "\vrumenz \vis \va \vcommunity \vof \vLinux \vNerds" rumenz
is
a
community
of
Linux
Nerds8. Combine \n and \v
$ echo -e "
\vrumenz
\vis
\va
\vcommunity
\vof
\vLinux
\vNerds" rumenz
is
a
community
of
Linux
Nerds9. Use -e with \r to return cursor to line start
$ echo -e "rumenz \ris a community of Linux Nerds" is a community of Linux Nerds10. Use -e with \c to suppress further output
$ echo -e "rumenz is a community \cof Linux Nerds" rumenz is a community rumenz@local:~$11. Use -n to omit the trailing newline
$ echo -n "rumenz is a community of Linux Nerds" rumenz is a community of Linux Nerdsrumenz@local:~/Documents$12. Use -e with \a for an audible alert
$ echo -e "rumenz is a community of \aLinux Nerds" rumenz is a community of Linux NerdsNote: Ensure the system volume is turned up before testing the alert.
13. Print all files in the current directory
$ echo * 103.odt 103.pdf 104.odt 104.pdf … network.jpeg14. Print only .jpeg files
$ echo *.jpeg network.jpeg15. Redirect echo output to a file
$ echo "rumenz.com" > 1.txt
cat 1.txt rumenz.comEcho Options
-n : do not print the trailing newline.
-e : enable interpretation of backslash escapes.
\b : backspace.
\ : backslash.
\n : new line.
\r : carriage return.
\t : horizontal tab.
\v : vertical tab.
These options can be combined and repeated as needed to format output precisely.
Images
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
