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.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
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
Even multiple strings can be printed without using single or double quotes.
$ echo follow linuxtldr.com
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 *
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
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 “
$ echo -n Hello, folks!
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
-e” flag, which enables the interpretation of backslash escapes (special characters).
Removing the Spaces Between the Text
\b” backspace with the backslash interpreter “
-e” will remove the spaces between the text.
$ echo -e "We \bAre \bLinux \bTLDR"
Removing the Trailing New Line
\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"
Creating a Trailing New Line
\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"
Creating a Horizontal Tab Space
\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"
Creating Vertical Tab Space
\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"
Carriage Return in Output
\r” carriage return with the backslash interpreter “
-e” will return the specified carriage in output.
$ echo -e "We Are \rLinux TLDR"
Alert Return with Sound
\a” alert return with the backslash interpreter “
-e” will have a sound alert.
$ echo -e "\aWe Are Linux TLDR"
|Print the message without the trailing newline|
|enable interpretation of backslash escapes|
|Removing the spaces between the text|
|omit the trailing newline|
|Creating a trailing newline|
|Creating horizontal tab space|
|Creating vertical tab space|
|Carriage return in output|
|Alert return with sound|
That was the end. Sayonara!