Beginners Guide for Echo Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The echo command takes the text or file as an argument and prints the output on the terminal screen.

It is mostly used in shell scripts when developers create a variable and use the variable to print the value on screen using the echo command.

Tutorial Details

DescriptionEcho command
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesecho
Internet RequiredNo
Discussed Tools in this Article

Syntax of the Echo Command

The echo command usually takes one argument as input if you are not using it with flags.

$ echo [OPTION] [STRING]

Also Read: List of Special Parameters in Bash with Examples

Printing a Text Message

You can use the echo command to print a string without using single or double quotes.

$ echo linuxtldr.com

Output:

Printing the single string
Printing the single string

Even multiple strings can be printed without using single or double quotes.

$ echo follow linuxtldr.com

Output:

Printing the multiple string
Printing the multiple strings

However, if you replace the argument with the “*” sign, it will output all the files and directories in your current working directory, acting similarly to the ls command.

$ echo *

Output:

Listing all the content in the current working directory
Listing all the content in the current working directory

Also Read: How to Set and List Environment Variables in Linux

Printing the PATH Environment Variables

All the commands you execute from your terminal are searched at the location that is added to your environment’s $PATH.

Execute the following command to view all the paths added to your environment variables.

$ echo $PATH

Output:

Printing the "$PATH" variable
Printing the “$PATH” variable

Printing a Message Without the Trailing New Line

When you execute the echo command to print the output on screen, it will appear in a newline because the echo command automatically instructs the output to be in a new line.

Although, you can easily omit the echoing trailing newline using the “-n” flag.

$ echo -n Hello, folks!

Output:

Printing the string without trailing line
Printing the string without a trailing line

Redirecting the Echo Output to the New File

The output of the echo command can be saved in a new or existing file using the file redirection signs.

$ echo Hello, folks! >> file.txt
$ cat file.txt 

Output:

Redirecting the echo output to a text file
Redirecting the echo output to a text file

Note: Further, you will learn to use the “-e” flag, which enables the interpretation of backslash escapes (special characters).

Removing the Spaces Between the Text

The “\b” backspace with the backslash interpreter “-e” will remove the spaces between the text.

$ echo -e "We \bAre \bLinux \bTLDR"

Output:

Removing space between text
Removing space between text

Removing the Trailing New Line

The “\c” suppresses the trailing new line with the backslash interpreter “-e“, which will add each line without emitting a new line.

$ echo -e "We Are Linux \cTLDR"

Output:

Removing the trailing new line
Removing the trailing new line

Creating a Trailing New Line

The “\n” trailing new line with the backslash interpreter “-e” will create a new line each time “\n” is specified.

$ echo -e "We \nAre \nLinux \nTLDR"

Output:

Creating trailing new line
Creating trailing new line

Creating a Horizontal Tab Space

The “\t” horizontal tab with the backslash interpreter “-e” will create a horizontal tab space each time it’s specified.

$ echo -e "We \tAre \tLinux \tTLDR"

Output:

Adding horizontal tab space between text
Adding horizontal tab space between text

Creating Vertical Tab Space

The “\v” vertical tab with the backslash interpreter “-e” will create a vertical tab space on each occasion it is specified.

$ echo -e "We \vAre \vLinux \vTLDR"

Output:

Adding vertical tab space between text
Adding vertical tab space between text

Carriage Return in Output

The “\r” carriage return with the backslash interpreter “-e” will return the specified carriage in output.

$ echo -e "We Are \rLinux TLDR"

Output:

Carriage return in output
Carriage return in output

Alert Return with Sound

The “\a” alert return with the backslash interpreter “-e” will have a sound alert.

$ echo -e "\aWe Are Linux TLDR"

Output:

Alert with sound
Alert with sound

Echo Options

 Options Description
-nPrint the message without the trailing newline
-eenable interpretation of backslash escapes
\bRemoving the spaces between the text
\\backslash
\comit the trailing newline
\nCreating a trailing newline
\tCreating horizontal tab space
\vCreating vertical tab space
\rCarriage return in output
\aAlert return with sound

That was the end. Sayonara!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.