Bash Echo Command

Bash echo is a command in bash shell that writes its arguments to standard output.

Whatever you see in the terminal is because of echo command being executed by other programs.

In this tutorial, we will go through the syntax of echo command, and understand its usage with the help of some example scripts.

Syntax

Following is the syntax of echo command

echo [option(s)] [string]
ADVERTISEMENT

Options Available with Echo Command

Following are the options available with echo command :

OptionDescriptionExample
-nTrailing outline is omittedecho -n “Learn Bash”
-EDisable interpretation of backslash escaped charactersecho -E “Learn\nBash”
-eEnable interpretation of backslash escaped charactersecho -e “Learn\nBash”

Examples

Terminal is a bash shell program. It can execute bash commands directly in the terminal or you may create a bash script file and run the file in terminal.

Example 1 – Simple Echo Example

Run the echo command in terminal as shown below.

sh-3.2# echo "Learn Bash"
Learn Bash

Following example demonstrates how to use echo in bash script file.

Bash Script

#!/bin/bash

echo "Learn Bash"

When you run above bash script file in Terminal, you will get the following output.

Output

sh-3.2# bash bash-echo-example 
Learn Bash

Example 2 – Echo Without Trailing Newline

In the following example, we will echo without trailing newline.

Run the echo command in the terminal, as shown below.

sh-3.2# echo -n "Learn Bash"
Learn Bash arjun@arjun-VPCEH26EN:~/bash$

Following example demonstrates how to use echo in bash script file.

Bash Script

#!/bin/bash

echo -n "Learn Bash"

When you run above bash script file in Terminal, you will get the following output.

Output

sh-3.2# bash bash-echo-example 
Learn Bash arjun@arjun-VPCEH26EN:~/bash$

Example 3 – Echo Interpreting Backslash Escaped Characters

In this example, we will use Echo command interpreting backslash escaped characters.

Run the echo command in the terminal, as shown below.

sh-3.2# echo -e "Learn\nBash"
Learn
Bash

Following example demonstrates how to use echo in bash script file.

Bash Script

#!/bin/bash

echo -e "Learn Bash"

When you run above bash script in Terminal, you will get the following as output.

Output

sh-3.2# bash bash-echo-example
Learn
Bash

Example 4 – Echo Without Interpreting Backslash Escaped Characters

In this example, we will use Echo command without interpretation of escaped characters.

Run the echo command in the terminal, as shown below.

sh-3.2# bash bash-echo-example
Learn\nBash

Following example demonstrates how to use echo in bash script file.

Bash Script

#!/bin/bash

echo -E "Learn Bash"

When you run above bash script in Terminal, you will get the following as output.

Output

sh-3.2# bash bash-echo-example
Learn\nBash

Conclusion

In this Bash TutorialBash Echo, we have learnt the syntax of echo command, options available with echo command, with example Bash Script Files.