The “echo” is the built-in command line utility used to display the input text or string in the terminal. It is generally used in batch files and shell scripting to print the specific input/string as an output in the terminal.
This post will demonstrate the working/usage of the echo command in Linux with the help of suitable examples: The outcomes of this guide are written below:
- How Does the “echo” Command Work in Linux?
- Display the Input/String
- Remove the Spaces From the String
- Print Each Letter of a String into a New Line
- Create Horizontal Spaces in a String
- Remove the Particular Part of String
- Print all Files and Directories
Let’s start with the working of the “echo” command.
How Does the “echo” Command Work in Linux?
The working of the “echo” command depends upon its syntax. The generalized syntax of the “echo” command is written below.
$ echo [option] [string]
The syntax contains the following components:
- echo: Represents the “echo” command.
- option: Supported option of “echo” command.
- string: Identifies the input string.
Execute the “man” command to get a detailed view of the “echo” command along with its supported options:
$ man echo
Now, move on to the working of the “echo” command with the help of various practical examples.
Example 1: Display the Input/String
The “echo” command without any argument displays the mentioned input string into the terminal as an output:
$ echo Welcome to its Linuxfoss
Example 2: Remove the Spaces From the String
The “echo” command offers the “\b” argument to remove all the species available in the output string. In this example, “\b” is used to remove the species between “Hello to the Linux”:
$ echo -e “Hello \bto \bthe \bLinux”
The “-e” option is mentioned in the above command to interpret the “backslash” operations.
The output confirms that all the spaces between the “Hello to the Linux” string have been removed.
Example 3: Print Each Letter of a String into a New Line
The “\n” argument prints every letter of the specified “string” into the new line. The practical implementation of this argument is shown below:
$ echo -e ‘This \nis \nthe \nnew \nline’
Example 4: Create Horizontal Spaces in a String
The “\t” flag is beneficial to create the horizontal tab spaces between an input string:
$ echo -e ‘Linux \tis \tan \toperating \tsystem’
In this case, the horizontal tab space has been added into the “Linux is an operating system” string.
Example 5: Remove the Particular Part of String
The “\r” removes the previous part of the string from which it is mentioned. In this example, the “\r” flag is used with the “in” letter in the below-mentioned string:
$ echo -e ‘tar tool is available \rin all Linux distributions’
The output verifies that the precious part from the “in” letter has been removed.
Example 6: Print all Files and Directories
The “*” asterisk(Wildcard) with the “echo” command displays all the files or directories in the specified directory.
Suppose to list down/print the “Directory1” content use the “echo *” command in this way:
The output shows the whole content available in the “Ditectory1”.
That’s all about the “echo” command in Linux.
The main goal of the “echo” command line tool is to print the input text/string specified as an argument. This tool is beneficial for displaying the text or string in the terminal differently. This task can be performed using the supported flags of the “echo” command. This guide has illustrated the objective, usage, and working of the “echo” command in Linux.