The echo command in Linux is an essential tool for scripting and programming. The purpose of the echo command in Linux primarily displays or prints various arguments on the terminal screen. Additionally, the command can be used to create and modify environment variables, manage file input/output, and execute shell commands. This is a highly versatile tool that can be utilized by developers and programmers for debugging, as well as constructing various products like websites, applications, and operating systems.
An echo command is a valuable tool for system administrators, as it can be, used to create shell scripts or batch files to automate various tasks. By mastering this echo in linux command, system administrators can greatly simplify their tasks and improve their productivity. In this guide, we will explore the various options available for using the echo command. We have tested our examples of echo commands on RHEL, Rocky Linux, AlamaLinux & Fedora.
The Global Syntax of echo command with options:
echo [OPTION] [input string]
The following tabular gives you the possible options in the echo command:
1. How to input a text to get the output using the echo command?
# echo LinuxTeck A complete learning blog
Output:- LinuxTeck A complete learning blog
Note: The above-stated "echo" command functions by displaying the specified text on the standard output. It does not manipulate the text or perform any operations on it; it simply prints the text as is.
2. How to print a variable?
# echo The value of x =$x
Output : The value of x = 100
Note: In this scenario, "x" is the designated variable name and its corresponding value is 100. To avoid any errors, it is crucial to ensure that there is no space between the variable and its value. With the Linux echo command, we can effortlessly print the value of "x" to the standard output for display.
3. How to create Alerts while printing?
# echo -e "LinuxTeck A complete learning blog\a"
Output: LinuxTeck A complete learning blog
Note: In our example of the "echo" command, we utilized the "-e" option to enable the interpretation of backslash escape sequences. By doing so, special characters such as tabs or new lines can be displayed correctly. Additionally, we included the '\a' sequence to generate an alert and produce a beep sound while the text is being printed.
4. How to add a new line?
# echo -e "LinuxTeck \nA complete learning Blog"
A complete learning Blog
Note: The Linux "echo" command provides the "-e" option, which instructs the command to interpret backslash escape sequences in the specified text. By utilizing the '\n' sequence, we can add a new line and print the subsequent text on a new line below the previous one. This enables us to format the output and make it more readable by separating it into distinct lines.
5. How to add a horizontal tab to your content?
# echo -e "LinuxTeck \tA \tComplete \tLearning \tBlog"
Output: LinuxTeck A Complete Learning Blog
Note: With the "-e" option of the Linux "echo" command, backslash escape sequences can be interpreted in the specified text. One such sequence is '\t', which creates a horizontal space between the content, similar to the functionality of the TAB key on a keyboard. This is useful for formatting the output and aligning the content in a more organized manner.
6. How to add a Vertical tab to your content?
# echo -e "LinuxTeck \vA \vComplete \vLearning \vBlog"
Output : LinuxTeck
Note: When using the "-e" option of the Linux "echo" command, backslash escape sequences can be interpreted in the specified text. One of these sequences is '\v', which creates a vertical space between the content. This functionality is similar to the TAB key on a keyboard, but it aligns the text vertically instead of horizontally.
7. How to suppress the content to display after a certain portion?
# echo -e "LinuxTeck A Complete Learning Blog \cLinux | AWS | VMware | DevOps"
Output: LinuxTeck A Complete Learning Blog
Note: When using the "-e" option of the Linux "echo" command, backslash escape sequences can be interpreted in the specified text. One of these sequences is '\c', which is primarily used to suppress the display of any content after this sequence. This means that if '\c' is included in the text provided to the "echo" command, any text following it will not be displayed on the standard output. This feature can be useful for filtering out unwanted text or for creating cleaner output in certain situations.
8. How to use Carriage Return?
# echo -e "LinuxTeck is \rA Complete Learning Blog"
Output: A Complete Learning Blog
Note: The "-e" option of the Linux "echo" command allows for the interpretation of backslash escape sequences in the text provided. One of these sequences is '\r', which essentially deletes any text that appears before it. This feature can be utilized to remove content from a specific portion of the output. In the echo example provided, the text "LinuxTeck" was removed from the output by including '\r' just before it in the text provided to the "echo" command.
9. How to use Backspace for your content?
# echo -e "LinuxTeck- \bA \bComplete \bLearning \bBlog"
Output : LinuxTeck-ACompleteLearningBlog
Note: With the "-e" option of the Linux "echo" command, backslash escape sequences can be interpreted in the provided text. One such sequence is '\b', which eliminates all spaces in between the content. In the given example, the text "A Complete Learning Blog" had all its spaces removed by including '\b' in the provided text to the "echo" command. This can be useful for formatting output and removing any unwanted spaces between words or sentences.
10. How to add Form Feed to your content?
# echo -e "LinuxTeck \fA \fComplete \fLearning \fBlog"
Note: The "-e" option of the Linux "echo" command allows for the interpretation of backslash escape sequences in the provided text. One such sequence is '\f', which separates output pages instead of using newlines. This functionality can be useful when generating output that needs to be formatted in a page-by-page manner. Instead of simply adding a newline character to separate content, '\f' will create a new page on the output.
11. How to print all the files and folders?
# echo *
Output : testfile1 testfile2 testfile3 testfile4 testfolder
Note: This "echo" command in Linux is similar to the "ls" command in that it provides information about files and folders. However, the "echo" command simply prints out the names of all files and folders, without any additional details. On the other hand, the "ls" command displays information about files and folders, including permissions, ownership, size, and modification time. Additionally, "ls" uses color combinations to help users quickly identify files and folders based on their types. By default, the "echo" command only displays the file and folder names in plain white color, without any additional information or formatting.
12. How to print some particular extensions of files?
Output: boot.log vmware-vmsvc.log wpa_supplicant.log Xorg.0.log Xorg.9.log yum.log
Note: The example above demonstrates how to find the extensions of all files with the ".log" extension.
13. How to redirect the output to a file with a nonstandard output?
# echo "LinuxTeck" > test
Output : # cat test
Note: The "echo" command in Linux can be used to redirect output to a text file instead of the standard output. This can be achieved by using the ">" symbol followed by the name of the file to which the output should be redirected. For example, the command "echo 'Hello World' > output.txt" will create a new file named "output.txt" and write the text "Hello World" to it, instead of printing it to the standard output. This can be useful for saving the output of a command to a file for later use or analysis.
Our article on the echo command in Linux has covered all 13 flags with corresponding examples. Thank you for taking the time to read it, and we hope that it has improved your understanding of the topic. If you have any feedback or comments, please share them with us. Additionally, if you find this article helpful, please feel free to share it with others.
To find all the RHEL / CentOS 7 articles, click here
To find all the Rocky Linux articles, click here
To find all the Ubuntu articles, click here
We collected some information from the echo man page.