What Is Echo in Shell Scripting?


Scott Campbell

Shell scripting is a powerful tool that allows us to automate tasks and execute commands within a shell environment. One of the most commonly used commands in shell scripting is echo.

The echo command is used to display text or variables on the standard output (usually the terminal). It can also be used to redirect output to a file or as input to another command.

Basic Usage

To use the echo command, simply type echo, followed by the text you want to display. For example:

$ echo "Hello, World!"
Hello, World!

This will display the text “Hello, World!” on your terminal.

Echoing Variables

The echo command can also be used to display the values of variables. To do this, simply prepend the variable name with a dollar sign ($). For example:

$ name="John Doe"
$ echo "My name is $name"
My name is John Doe

In this example, we assign the value “John Doe” to the variable name. Then, we use the echo command to display the text “My name is John Doe” on the terminal.

Escape Characters

Sometimes, you may want to include special characters or control sequences in your echoed text. To achieve this, you can use escape characters. Escape characters are preceded by a backslash (\) and change how certain characters are interpreted by the shell.

Note: In HTML code snippets below, escape characters are represented using HTML entities for better visualization.

Here are some common escape characters:

  • \n: Newline character – starts a new line
  • \t: Tab character – inserts a tab space
  • \\: Backslash – used to display a literal backslash
  • \": Double quote – used to display a literal double quote
  • \$: Dollar sign – used to display a literal dollar sign

For example, let’s say we want to display the following text:

This is line one.
This is line two.

We can achieve this using the escape character for newlines:

$ echo "This is line one.\nThis is line two." This is line one. 

This is line two. 

The output will be displayed with each sentence on a separate line.

Redirecting Output and Using Pipes

The output of the echo command can be redirected to a file using the > operator. For example:

$ echo "Hello, World!" > hello.txt
$ cat hello.txt
Hello, World!

In this example, the text “Hello, World!” is written to a file called hello.txt. Then, we use the cat command to display the contents of the file on the terminal.

The output of the echo command can also be used as input for another command using pipes (|). For example:

$ echo "Hello" | wc -c

In this example, the wc -c command is used to count the number of characters in the text “Hello” that is passed from the echo command via a pipe. The output of the wc -c command is the number 6, which represents the number of characters in the text.


The echo command is a fundamental tool in shell scripting that allows you to display text or variables on the terminal. It can be used to automate tasks, create interactive scripts, and redirect output to files or other commands. By using escape characters, redirecting output, and utilizing pipes, you can make your shell scripts even more powerful and efficient.

So go ahead and start experimenting with the echo command in your shell scripts. With its simplicity and versatility, you’ll be able to create dynamic and interactive scripts in no time!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy