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
Description | Echo command |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | echo |
Internet Required | No |
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:
Even multiple strings can be printed without using single or double quotes.
$ echo follow linuxtldr.com
Output:
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:
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 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:
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:
-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 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:
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 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:
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:
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:
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:
Echo Options
Options | Description |
-n | Print the message without the trailing newline |
-e | enable interpretation of backslash escapes |
\b | Removing the spaces between the text |
\\ | backslash |
\c | omit the trailing newline |
\n | Creating a trailing newline |
\t | Creating horizontal tab space |
\v | Creating vertical tab space |
\r | Carriage return in output |
\a | Alert 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.