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.
Following are the topics we shall go through in this tutorial :
- Syntax
- Simple Echo Example
- Example-1 – Echo without trailing newline
- Example-2 – Echo interpreting backslash escaped characters
- Example-3 – Echo without interpretation
Syntax
Following is the syntax of echo command
echo [option(s)] [string]
Options Available with Echo Command
Following are the options available with echo command :
Option | Description | Example |
-n | Trailing outline is omitted | echo -n “Learn Bash” |
-E | Disable interpretation of backslash escaped characters | echo -E “Learn\nBash” |
-e | Enable interpretation of backslash escaped characters | echo -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.
$ 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
$ ./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.
$ 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
$ ./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.
$ 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
$ ./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.
$ echo -E "Learn\nBash" 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
arjun@arjun-VPCEH26EN:~/bash$ ./bash-echo-example Learn\nBash
Conclusion
In this Bash Tutorial – Bash Echo, we have learnt the syntax of echo command, options available with echo command, with example Bash Script Files.