How to Use the echo Command in Linux
Introduction
The echo
command in Linux is used to display text or variables to the standard output (STDOUT). It's commonly used in shell scripts, command pipelines, and interactive shell sessions to print messages, display variables, or generate output for further processing.
This article explains how to use the echo
command in Linux to display and manipulate text.
Syntax of echo
Command
Below is the basic echo
command syntax:
$ echo [options] [text...]
Within the above command, [options]
includes optional flags that modify the command's behavior, and [text...]
sets the text or variables to display. Multiple text arguments are separated with spaces.
echo
Command Options
Option | Description |
---|---|
-e |
Enables the interpretation of backslash escapes. |
-n |
Disables output of trailing newline characters. |
Practical Examples of the echo
Command
Display a sample message using the
echo
command.console$ echo "Hello, World!"
The above command outputs the message
Hello, World!
.Output:
Create a new variable and display it's value using the
echo
command.console$ name="John" && echo "Welcome, $name"
The above command outputs the message
Welcome, John
by substituting the value of the variable$name
into the output.Output:
Display a new message with the system date.
console$ echo "Today is" $(date)
The above command outputs the message
Today is
and the output of thedate
command which displays the system date and time.Output:
Display special characters.
console$ echo -e "Line 1 \nLine 2 \nLine 3"
The above command outputs three lines with the newline character
\n
sequence.Output:
Display text without a trailing newline.
console$ echo -n "Hello, " $ echo "World!"
The above command outputs
Hello, World!
without a newline character afterHello,
.Output:
Display content without spaces between text.
console$ echo -e "This \bis \ba \bexample \btext"
The above command outputs the backspace escape character
\b
to remove spaces between words.Output:
Display tabs between text.
console$ echo -e "This \tis \ta \texample \ttext"
The above command output the tab escape character
\t
to insert tabs between words.Output:
Display all files and folders in the working directory.
console$ echo *
The above command outputs all files and directories in the active working directory similar to the
ls
command.Output:
Advanced Usage Scenarios
Combine
echo
with command substitution.console$ echo "The current directory is: $(pwd)"
The above command outputs the message
The current directory is:
and the output of thepwd
command which displays the active working directory.Output:
Redirect the
echo
command output to a file.console$ echo "Hello, World!" > output.txt
The above command outputs the command output
Hello, World!
to a new fileoutput.txt
.Output:
Display environment variables.
console$ echo "PATH: $PATH"
The above command outputs the value of the
PATH
environment variable.Output:
Display a colored output.
console$ echo -e "\e[31mThis is red text\e[0m"
The above command outputs the text in red using escape sequences to enabled a colored output.
Output:
Using
echo
in a script:Create a new sample script file
my_script.sh
using a text editor such as Nanoconsole$ nano my_script.sh
Add the following contents to the file.
bash#!/bin/bash echo "Starting the script..." echo "Current directory: $(pwd)" echo "Script completed."
Enable execute privileges on the file.
console$ chmod +x my_script.sh
Run the script
console$ ./my_script.sh
The above script uses the
echo
command to display messages at different intervals.Output:
Conclusion
You have used the echo
command in Linux to efficiently display text, variables, and messages. For more command options, run the man echo
command to view the echo
manual pages.