Echo Command in Linux: Usage and Examples

Echo Command in Linux: Usage and Examples

A short status message is often all a shell script needs to show while it runs. The Bash echo command writes its arguments to standard output and normally adds a newline.

I’ll show how to print text and variables, then explain when echo behaves differently across shells.

TL;DR: Echo command in Linux

The Bash echo command writes its arguments to standard output, normally followed by a newline.

  • echo text prints a short message.
  • echo -n text omits the trailing newline in Bash.
  • Use printf for predictable formatting across shells.

What is the echo command in Linux?

Bash’s echo builtin writes arguments to standard output. This is the stream programs use for their normal results.

Command formEffect in Bash
echo HelloWrites Hello and a newline.
echo -n HelloWrites Hello without its final newline.
echo -e with a newline escapeInterprets supported backslash escapes when enabled.

Bash separates echo’s arguments with spaces and ends its output with a newline unless -n is used.

How to use echo command in Linux step by step

Choose echo for short messages. I ran the examples in a named Bash script, and the capture shows their output.

Step 1: Print a string

Use single quotes when dollar signs or backslashes must remain literal. In Bash, double quotes still allow variable expansion.

echo 'Hello, Linux!'
echo 'service ready'
echo 'backup complete'

These lines print separate status messages, each followed by a newline.

Step 2: Print a variable

It removes the quote marks before passing the value to echo.

echo "$HOME"

The command echoes the expanded HOME value, shown here as /home/ubuntu. For more readable formats, choose printf.

Step 3: Control the final newline

The -n option suppresses Bash echo’s usual trailing newline, so the next command can continue on the same line.

echo -n 'Ready'
printf '|next\n'

The capture shows Ready|next because the first command suppressed its newline. printf marks the next line break in its format string.

Step 4: Redirect output to a file

When you need a message later, redirect it to a file. A single > replaces the file contents, while >> adds text at the end, so check which behavior suits the task.

echo 'status=ready' > status.txt
printf '%s\n' 'checked' >> status.txt
cat status.txt

The step writes one status line, then appends a second line before displaying the file. I used append for the check result to preserve the earlier message.

For a small text file, display the contents with head or use tail to inspect its final lines.

Bash echo and printf terminal output

Step 5: Print several arguments

Extra spaces typed between unquoted words are not preserved. A quoted phrase remains one argument and keeps its internal spacing.

echo 'Linux shell' command

The output is Linux shell command. This shows that shell quoting groups the phrase before echo receives it.

Echo command behavior and portability limits

Bash provides -e for escape interpretation. POSIX leaves some option and backslash cases implementation-defined. I compared the Bash builtin with xpg_echo on and off.

CaseWhat to knowPractical choice
-e and backslashesBash supports escape interpretation. The xpg_echo setting and other shells can change the result.Use printf for portable escapes.
echo starThe shell expands the wildcard before echo runs. With no matches, the shell may leave the wildcard unchanged or remove it, depending on settings.Use ls to inspect directory entries.
POSIX scriptsPOSIX does not require echo options, and some leading-dash or backslash inputs have implementation-defined results.Use printf for new scripts needing consistent behavior.
Backspace escapebackslash-b is the backspace character, whose visible effect depends on the output device.Do not use it as text editing.

The Bash reference documents options such as -n and -e. I checked it alongside the POSIX definition before choosing which examples to include.

I ran this command in Bash, then checked the two output lines against the terminal capture.

bash -c 'shopt -u xpg_echo; echo -e "first\\nsecond"; shopt -s xpg_echo; echo -e "first\\nsecond"'

The terminal showed first and second on separate lines, then printed the same pair again. In this Bash run, enabling xpg_echo did not change those outputs because echo -e already requested escape interpretation.

For a literal character, Bash uses its ANSI-C quoting form, such as $’text\nnext’. This syntax belongs to Bash.

For new scripts, choose printf when escapes or exact field formatting matter. Its format string describes the output directly.

Conclusion: Use echo for simple output

Echo prints short messages or values. For exact formatting, choose printf and check its shell documentation.

FAQ

These answers explain echo’s usual output and common portability questions.

What does the echo command do in Linux?

It writes its arguments to standard output and normally ends with a newline.

How do I suppress the newline with echo?

In Bash, pass -n, as in echo -n ‘Ready’. Shell implementations vary, so use printf for portable formatting.

Why does echo -e behave differently?

Escape handling depends on the shell, its settings, and the echo implementation. Bash’s xpg_echo setting changes escape interpretation defaults.

Does echo * list files?

The shell expands the wildcard before echo receives its arguments. Use ls when you need a directory listing.