Fundamentals 7 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Linux echo Command: Syntax, Options, and Practical Examples

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 Nerds

Output:

rumenz is a community of Linux Nerds

2. Declare a variable and print its value

$ x=10
$ echo The value of variable x = $x

Output:

The value of variable x = 10

3. Use -e with \b (backspace) to remove spaces

$ echo -e "rumenz \bis \ba \bcommunity \bof \bLinux \bNerds"
rumenzisacommunityofLinuxNerds

4. Use -e with \n for new lines

$ echo -e "rumenz 
is 
a 
community 
of 
Linux 
Nerds"
rumenz 
 is 
 a 
 community 
 of 
 Linux 
 Nerds

5. Use -e with \t for horizontal tabs

$ echo -e "rumenz \tis \ta \tcommunity \tof \tLinux \tNerds"
rumenz  is  a  community  of  Linux  Nerds

6. Combine \n and \t

$ echo -e "
\trumenz 
\tis 
\ta 
\tcommunity 
\tof 
\tLinux 
\tNerds"
rumenz 
        is 
        a 
        community 
        of 
        Linux 
        Nerds

7. Use -e with \v (vertical tab)

$ echo -e "\vrumenz \vis \va \vcommunity \vof \vLinux \vNerds"
rumenz 
        is 
           a 
                community 
                     of 
                      Linux 
                        Nerds

8. Combine \n and \v

$ echo -e "
\vrumenz 
\vis 
\va 
\vcommunity 
\vof 
\vLinux 
\vNerds"
rumenz 

is 

a 

community 

of 

Linux 

Nerds

9. Use -e with \r to return cursor to line start

$ echo -e "rumenz \ris a community of Linux Nerds"
is a community of Linux Nerds

10. 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 Nerds

Note: 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.jpeg

14. Print only .jpeg files

$ echo *.jpeg
network.jpeg

15. Redirect echo output to a file

$ echo "rumenz.com" > 1.txt
cat 1.txt
rumenz.com

Echo 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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxtutorialbashcommand-lineEchoOptions
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.