🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

echo Command

Beginner Shell Scripting man(1)

Display a line of text to the terminal

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
echo [OPTION]... [STRING]...

What Does echo Do?

echo displays a line of text or variable values to standard output. It is one of the most basic and frequently used commands in Linux, essential for scripts, debugging, and generating output.

echo supports escape sequences (with -e flag) for formatting including newlines, tabs, and colors. It is used extensively in shell scripts for printing messages, generating file content, and building output strings.

There are two versions: the shell built-in echo and /bin/echo. The built-in version varies slightly between bash, zsh, and other shells. printf is often preferred for consistent behavior across shells.

Options & Flags

OptionDescriptionExample
-n Do not output trailing newline echo -n "Enter name: "
-e Enable interpretation of escape sequences echo -e "Line1\nLine2"
-E Disable escape sequences (default) echo -E "No \n here"
\n Newline (with -e) echo -e "Hello\nWorld"
\t Tab (with -e) echo -e "Col1\tCol2"
\033[ ANSI color codes (with -e) echo -e "\033[32mGreen text\033[0m"

Practical Examples

#1 Print text

Prints text to standard output.
$ echo "Hello World"
Output: Hello World

#2 Print variable

Prints the value of the HOME environment variable.
$ echo "Home is: $HOME"
Output: Home is: /home/user

#3 No trailing newline

Prints without newline — useful for inline prompts.
$ echo -n "Enter your name: "

#4 Write to file

Creates (or overwrites) a file with the text.
$ echo "server=192.168.1.1" > config.txt

#5 Append to file

Appends a timestamped message to a log file.
$ echo "$(date): Deploy complete" >> deploy.log

#6 Multiple lines

Outputs multiple lines using escape sequences.
$ echo -e "Line 1\nLine 2\nLine 3"
Output: Line 1 Line 2 Line 3

#7 Colored output

Prints "ERROR:" in bold red followed by normal text.
$ echo -e "\033[1;31mERROR:\033[0m Something failed"

#8 Tab-formatted output

Creates a tab-separated table.
$ echo -e "Name\tAge\tCity\nAlice\t30\tLondon"
Output: Name Age City Alice 30 London

Tips & Best Practices

Use printf for consistency: echo behavior varies between shells. printf behaves consistently: printf "%s\n" "Hello" works the same everywhere.
Single vs double quotes: Double quotes expand variables: echo "". Single quotes are literal: echo '$HOME' prints $HOME literally.
-e is not universal: Not all echo implementations support -e. In scripts targeting multiple systems, use printf instead for escape sequences.

Frequently Asked Questions

How do I print without a newline?
Use echo -n "text" to omit the trailing newline. Or use printf "text" which does not add a newline by default.
How do I write to a file with echo?
Use echo "text" > file to create/overwrite, or echo "text" >> file to append.
What is the difference between echo and printf?
echo is simpler but varies between shells. printf is consistent across all POSIX shells and supports format specifiers like %s, %d, %f.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →