Quick take: The echo command prints text and variables to the terminal. Use -n to omit the trailing newline and -e to enable escape sequences like \n and \t. Combine it with redirection to write files.

Introduction

The echo command writes its arguments to standard output. It is the simplest way to print a message, show the value of a variable, or write a line into a file — which makes it one of the most-used commands in shell scripts.

This guide covers printing text and variables, controlling newlines and escape sequences, and using echo with redirection to create and append to files.

Syntax

The basic syntax of the echo command is:

echo [OPTIONS] [STRING...]

Common Options and Parameters

The most useful options and parameters for the echo command:

OptionDescription
-nDo not output the trailing newline.
-eEnable interpretation of backslash escapes (\n, \t, \\).
-EDisable escape interpretation (the default).

Practical Examples

Real echo commands you can run today:

# Print a simple message
echo 'Hello, world'
# Print the value of a variable
echo "Home is $HOME"
# Print without a trailing newline
echo -n 'Loading...'
# Use escape sequences (tab and newline)
echo -e 'Name:\tIrfan\nRole:\tEngineer'
# Write text to a new file
echo 'server { listen 80; }' > site.conf
# Append a line to a file
echo '127.0.0.1 myapp.local' >> /etc/hosts

echo in Shell Scripts

In scripts, echo does the everyday work of reporting progress, writing configuration, and building files line by line. Combined with redirection it can generate entire config files without an editor.

# Generate a small config file
echo 'PORT=8080' > app.env
echo 'DEBUG=false' >> app.env

# Report progress in a script
echo "Deploying version $VERSION..."

# Print a value to verify a variable is set
echo "DB host is: ${DB_HOST:-not set}"

Use double quotes when you want variables expanded and single quotes for literal text. The difference between > (overwrite) and >> (append) is critical here — mixing them up can wipe a file you meant to add to.

echo Portability and printf

echo behaves differently across shells, which causes subtle bugs. In some shells echo -e enables escape sequences; in others the -e is printed literally. For anything beyond a plain message, printf is the portable, predictable choice.

# printf gives consistent formatting everywhere
printf 'Name:\t%s\nRole:\t%s\n' "Irfan" "Engineer"

printf does not add a trailing newline automatically, so you include \n yourself — which is exactly why it is reliable. Use echo for quick interactive messages and printf for formatted output in scripts you want to run anywhere.

Tips and Best Practices

  • Use double quotes when you want variables expanded ("$USER") and single quotes to print text literally.
  • Remember > overwrites a file while >> appends — a common source of accidental data loss.
  • For portable scripts that need consistent escape handling, consider printf instead of echo -e.

Final Thoughts

echo is small but everywhere — printing messages, inspecting variables, and building files line by line in scripts. Remember -n to suppress the newline, -e for escape sequences, and the difference between > and >> when redirecting. For complex formatting, graduate to printf.

FAQ: echo Command in Linux

How do I print a variable with echo?+

Use double quotes so the shell expands it: echo "User is $USER". Single quotes would print the text literally, including the dollar sign.

How do I echo without a newline?+

Use the -n flag: echo -n 'text' prints without moving to the next line, which is useful for prompts and progress messages.

How do I add a tab or newline with echo?+

Enable escapes with -e: echo -e 'col1\tcol2' inserts a tab and echo -e 'line1\nline2' inserts a newline.

How do I write text to a file with echo?+

Redirect the output: echo 'text' > file overwrites the file, while echo 'text' >> file appends a new line to it.

What is the difference between echo and printf?+

echo is simpler and adds a newline automatically, but escape handling varies between shells. printf gives precise, portable formatting (like C's printf) and is preferred for anything beyond basic messages.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support