The Wayback Machine - https://web.archive.org/web/20240627065718/https://www.geeksforgeeks.org/echo-command-in-linux-with-examples/
Open In App

echo command in Linux with Examples

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file.

Syntax of `echo` command in Linux

echo [option] [string]

Here,

[options] = The various options available for modifying the behavior of the `echo` command

[string] = It is the string that we want to display.

Basic Usage: Displaying Text/String:

The most straightforward usage of the echo command is to display a text or string on the terminal. To do this, you simply provide the desired text or string as an argument to the echo command.

Syntax:

echo [string]

Example:

If we want to display “Geeks for Geeks”. We use the following command.

echo "Geeks for Geeks"

Display text

Display text

Options Available in `echo` command in Linux

NOTE :- -e here enables the interpretation of backslash escapes 

1. \b : it removes all the spaces in between the text

Example:

echo -e "Geeks \bfor \bGeeks"

remove space

remove space

2. \c : suppress trailing new line with backspace interpreter ‘-e‘ to continue without emitting new line.

Example:

echo -e "Geeks \cfor Geeks"

continue without emitting new line

continue without emitting new line

In the above example, text after \c is not printed and omitted trailing new line. 

3. \n : this option creates a new line from where it is used.

Example:

 echo -e "Geeks \nfor \nGeeks"

create new line

create new line

4. \t : this option is used to create horizontal tab spaces.

Example:

echo -e "Geeks \tfor \tGeeks"

creating horizontal tab space

creating horizontal tab space

5. \r : carriage return with backspace interpreter ‘-e‘ to have specified carriage return in output.

Example:

echo -e "Geeks \rfor Geeks"

Carriage return

Carriage return

In the above example, text before \r is not printed. 

6. \v : this option is used to create vertical tab spaces.

Example:

echo -e "Geeks \vfor \vGeeks

create vertical tab spaces

create vertical tab spaces

7. \a : alert return with backspace interpreter ‘-e‘ to have sound alert.

Example:

echo -e "\aGeeks for Geeks"

alert return

alert return

 This command when executed, it will produce an alert sound or Bell. 

8. echo *: this command will print all files/folders, similar to ls command.

Example:

echo *

similar to `ls` command

similar to `ls` command

 

9. -n: this option is used to omit echoing trailing newline.

Example:

echo -n "Geeks for Geeks"

omit echoing trailing newline.

omit echoing trailing newline

10. Redirecting `echo` Output

The output of the `echo` can be redirected to a file instead of displaying it on the terminal. We can achive this by using the `>` or `>>` operators for output redirection.

Example:

echo "Welcome GFG" > output.txt

This will write the output of the echo command to the file name `output.txt`. File will be overwritten if it already exists.

If we want to append the output in an existing file, we use `>>` instead of `>`.

Conclusion

In this article we have discussed `echo` command in Linux which is a powerful and versatile tool and allows users to display lines of the text or strings on the terminal. Overall, we can say that by understanding the `echo` command experimenting with its features, we can effectively display message, variables, or any desired text in our Linux.

?t=153


Previous Article
Next Article

Similar Reads

Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user's shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes. echo "$SHELL" will expand the $S
4 min read
Batch Script - Echo Command
Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on the console or in a text file. In batch scripting,
8 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally three options:-n: It does not print the trailing n
2 min read
How to Change the Output Color of Echo in Linux
When working with a computer, some users may encounter a situation where they need to output text in a different color. The colors we are familiar with by now will have been worked out for our particular machine and thus can be replaced easily if needed. Echo is a Linux command that can be used to change the output color of the text. Any text enter
9 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to make an Echo server with Bash?
An Echo server is a simple network interface/application that listens for any incoming connections and requests, and then echoes back any data it receives from clients. Creating such a server is a great way to learn about network programming and the basics of client-server communication. In this article, we will guide you through the process of bui
5 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
7 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
Article Tags :