Bash Echo Command
Bash echo is a shell built-in command used to print text, variable values, command output, and simple messages to standard output.
You commonly use echo in Bash scripts to display status messages, show variable values while debugging, write simple output to a file, or combine text with command substitutions.
In this tutorial, we will learn the syntax of the Bash echo command, its commonly used options, how newline handling works, and how to use echo with variables and output redirection.
Bash Echo Command Syntax
Following is the syntax of echo command
echo [option(s)] [string]
The string can be plain text, a quoted string, a variable, a command substitution, or a combination of these. In most Bash scripts, it is safer to quote text and variables so that spaces are preserved correctly.
echo "message"
echo "$variable_name"
echo "User: $USER"
Bash Echo Options for Newline and Escape Handling
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” |
The -n option prints output without adding the final newline. The -e option allows escape sequences such as \n and \t to be interpreted. The -E option disables that interpretation.
For portable shell scripts where output formatting must be exact, printf is often preferred. However, echo is still widely used for simple messages and quick script output.
Bash Echo Examples in Terminal and Script Files
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 – Print a Simple Message with Bash Echo
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 – Use Bash Echo Without a 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$
Notice that the shell prompt appears immediately after the printed text because echo -n does not add a newline at the end.
Example 3 – Interpret Backslash Escape Characters with Bash Echo -e
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
For a script version that prints the two words on separate lines, include the newline escape sequence inside the quoted string.
#!/bin/bash
echo -e "Learn\nBash"
Example 4 – Print Backslash Characters Literally with Bash Echo -E
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
To clearly see the effect of -E, pass a string that contains a backslash escape sequence. Bash prints \n as literal characters instead of converting it into a line break.
#!/bin/bash
echo -E "Learn\nBash"
Bash Echo with Variables
One of the most common uses of echo in shell scripts is to print variable values. Use double quotes when printing variables so that values containing spaces are handled as one string.
#!/bin/bash
course="Bash Shell Scripting"
echo "Course: $course"
Output
Course: Bash Shell Scripting
Bash Echo with Command Substitution
You can use command substitution inside echo to print the result of another command. Command substitution is written using $(command).
#!/bin/bash
echo "Current directory: $(pwd)"
This prints the text followed by the current working directory. The exact output depends on the directory from which you run the script.
Bash Echo Output Redirection to a File
The output of echo can be redirected to a file. Use > to create or overwrite a file, and use >> to append to an existing file.
echo "First line" > notes.txt
echo "Second line" >> notes.txt
cat notes.txt
Output
First line
Second line
Common Bash Echo Escape Sequences
When echo -e is used, Bash can interpret several escape sequences. The most useful ones are newline and tab formatting.
| Escape sequence | Meaning | Example use |
|---|---|---|
\n | New line | echo -e "Line 1\nLine 2" |
\t | Horizontal tab | echo -e "Name\tScore" |
\\ | Backslash character | echo -e "Path: C:\\Temp" |
Bash Echo Notes for Reliable Script Output
- Use double quotes around strings that contain spaces or variables.
- Use
echo -nonly when you intentionally do not want the final newline. - Use
echo -ewhen you need escape sequences such as\nor\t. - Use
echo -Ewhen backslashes should be printed as normal characters. - Use
printfinstead ofechowhen you need strict, portable formatting across different shells.
Bash Echo Command FAQs
What does echo do in Bash?
In Bash, echo writes its arguments to standard output. It is commonly used to print messages, variable values, and simple formatted output in shell scripts.
How do I print a new line with Bash echo?
Use echo -e with the \n escape sequence. For example, echo -e "Line 1\nLine 2" prints the two lines separately.
How do I use echo without adding a newline?
Use the -n option. For example, echo -n "Processing..." prints the text and keeps the cursor on the same line.
What is the difference between echo -e and echo -E?
echo -e enables interpretation of backslash escapes such as \n. echo -E disables that interpretation and prints backslashes literally.
Should I use echo or printf in Bash scripts?
Use echo for simple messages. Use printf when exact formatting, portability, or predictable handling of escape sequences is required.
Bash Echo Tutorial QA Checklist
- Confirm that every
echo -eexample contains the escape sequence that is being demonstrated. - Check that output-only blocks show terminal output and not extra commands.
- Verify that examples using variables quote the variable expansion.
- Ensure
>and>>redirection examples explain overwrite versus append behavior. - Keep the distinction between Bash
echoand more portableprintfclear.
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.
The Bash echo command is useful for printing simple messages, showing variable values, controlling newline behavior, interpreting escape sequences, and writing small amounts of text to files. For strict formatting needs, consider using printf.
TutorialKart.com