The echo command is one of the most commonly and widely used built-in commands for Linux bash and C shells, that typically used in a scripting language and batch files to display a line of text/string on standard output or a file.
The syntax for the echo command is:
echo [option(s)] [string(s)]
1. Input a line of text and display it on standard output
$ echo Tecmint is a community of Linux Nerds
Outputs the following text:
Tecmint is a community of Linux Nerds
2. Declare a variable and echo its value. For example, Declare a variable of x and assign its value=10.
$ x=10
echo its value:
$ echo The value of variable x = $x The value of variable x = 10
Note: The ‘-e‘ option in Linux acts as an interpretation of escaped characters that are backslashed.
3. Using option ‘\b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between.
$ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds" TecmintisacommunityofLinuxNerds
4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.
$ echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds" Tecmint is a community of Linux Nerds
5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab spaces.
$ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds" Tecmint is a community of Linux Nerds
6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.
$ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds" Tecmint is a community of Linux Nerds
7. Using option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab spaces.
$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds" Tecmint is a community of Linux Nerds
8. How about using option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously.
$ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds" Tecmint is a community of Linux Nerds
Note: We can double the vertical tab, horizontal tab, and new line spacing using the option two times or as many times as required.
9. Using option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.
$ echo -e "Tecmint \ris a community of Linux Nerds" is a community of Linux Nerds
10. Using option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.
$ echo -e "Tecmint is a community \cof Linux Nerds" Tecmint is a community avi@tecmint:~$
11. Omit echoing trailing new line using the option ‘-n‘.
$ echo -n "Tecmint is a community of Linux Nerds" Tecmint is a community of Linux Nerdsavi@tecmint:~/Documents$
12. Using option ‘\a‘ – alert return with backspace interpretor ‘-e‘ to have the sound alert.
$ echo -e "Tecmint is a community of \aLinux Nerds" Tecmint is a community of Linux Nerds
Note: Make sure to check the Volume key, before firing.
13. Print all the files/folders using echo command (ls command alternative).
$ echo * 103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt 109.pdf 110b.odt 110.odt 110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb network.jpeg
14. Print files of a specific kind. For example, let’s assume you want to print all ‘.jpeg‘ files, use the following command.
$ echo *.jpeg network.jpeg
15. The echo can be used with a redirect operator to output to a file and not standard output.
$ echo "Test Page" > testpage ## Check Content avi@tecmint:~$ cat testpage Test Page
echo Options
?Options | ?Description |
?-n | ?do not print the trailing newline. |
?-e | ?enable interpretation of backslash escapes. |
?\b | ?backspace |
?\\ | ?backslash |
?\n | ?new line |
?\r | ?carriage return |
?\t | ?horizontal tab |
?\v | ?vertical tab |
That’s all for now and don’t forget to provide us with your valuable feedback in the comments below.
The above is the detailed content of 15 Practical Examples of 'echo' command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three ways to create empty files in the command line: First, the simplest and safest use of the touch command, which is suitable for debugging scripts or placeholder files; Second, it is quickly created through > redirection but will clear existing content, which is suitable for initializing log files; Third, use echo"> file name to create a file with an empty string, or use echo-n""> file name to avoid line breaks. These three methods have their own applicable scenarios, and choosing the right method can help you complete the task more efficiently.

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

Eclipse is a free integrated development environment (IDE) that programmers around the world use to write software, primarily in Java, but also in other major programming languages using Eclipse plugins.The latest release of Eclipse IDE 2023?06 does

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

Linux has a rich collection of commands, and while many of them are powerful and useful for various tasks, there are also some funny and whimsical commands that you can try out for amusement. 1. sl Command (Steam Locomotive) You might be aware of the

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

PPA is an important tool for Ubuntu users to expand their software sources. 1. When searching for PPA, you should visit Launchpad.net, confirm the official PPA in the project official website or document, and read the description and user comments to ensure its security and maintenance status; 2. Add PPA to use the terminal command sudoadd-apt-repositoryppa:/, and then run sudoaptupdate to update the package list; 3. Manage PPAs to view the added list through the grep command, use the --remove parameter to remove or manually delete the .list file to avoid problems caused by incompatibility or stopping updates; 4. Use PPA to weigh the necessity and prioritize the situations that the official does not provide or require a new version of the software.

Gogo is a remarkable tool to bookmark directories inside your Linux shell. It helps you create shortcuts for long and complex paths in Linux. This way, you no longer need to type or memorize lengthy paths on Linux.For example, if there's a directory
