Beginners Guide for Echo Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 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
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesecho
Internet RequiredNo

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]

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

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

$ echo follow linuxtldr.com

Output:

Printing the multiple string

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

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

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
πŸ“
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 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

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

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

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

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

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.