echo Command in Linux: Print Text and Variables

By 

Updated on

9 min read

Stylized terminal showing the echo command printing Hello, Linux

The echo command is one of the most basic and frequently used commands in Linux. It prints its arguments to the standard output.

echo is commonly used in shell scripts to display messages or output the results of other commands. This guide covers the Bash builtin version of echo with practical examples.

Syntax

txt
echo [OPTIONS] [ARGUMENTS]

echo is a shell builtin in Bash and most other popular shells like Zsh and Ksh. There is also a standalone /usr/bin/echo utility, but the shell builtin version takes precedence.

The available options are:

  • -n - Do not output a trailing newline.
  • -e - Enable interpretation of backslash escape sequences.
  • -E - Disable interpretation of escape sequences (this is the default).

When -e is used, the following escape sequences are recognized:

  • \\ - Backslash.
  • \a - Alert (BEL).
  • \b - Backspace.
  • \c - Suppress further output.
  • \e, \E - Escape character.
  • \f - Form feed.
  • \n - New line.
  • \r - Carriage return.
  • \t - Horizontal tab.
  • \v - Vertical tab.
  • \0nnn - The character with the octal value nnn (zero to three digits).
  • \xHH - The character with the hexadecimal value HH (one or two digits).
  • \uHHHH - The Unicode character with the hexadecimal value HHHH (one to four digits).
  • \UHHHHHHHH - The Unicode character with the hexadecimal value HHHHHHHH (one to eight digits).

Display a String

To display a simple line of text, pass it as an argument to echo:

Terminal
echo Hello, World!
output
Hello, World!

The command prints the text exactly as it was passed to echo and adds a newline at the end.

Although not required, it is a good practice to enclose arguments in double or single quotes. When using single quotes '', the literal value of each character is preserved and variables are not expanded.

Display Strings with Quotes

To print a double quote, enclose the text within single quotes or escape it with a backslash:

Terminal
echo 'Hello "Linuxize"'
echo "Hello \"Linuxize\""
output
Hello "Linuxize"
Hello "Linuxize"

Both commands print the same text. The first one uses single quotes around the full string, and the second one escapes the double quotes with backslashes.

To print a single quote, enclose the text within double quotes:

Terminal
echo "I'm a Linux user."
output
I'm a Linux user.

Because the string is enclosed in double quotes, the single quote is printed as a regular character.

Use Escape Characters

Use the -e option to enable interpretation of escape sequences. In the following example, we are using \n for a new line and \t for a horizontal tab:

Terminal
echo -e "You know nothing, Jon Snow.\n\t- Ygritte"
output
You know nothing, Jon Snow.
	- Ygritte

The \n sequence moves the second part of the text to a new line, and \t adds a tab before the attribution.

Suppress the Trailing Newline

By default, echo appends a newline character at the end of the output. Use the -n option to suppress it:

Terminal
echo -n "Hello, " && echo "World!"
output
Hello, World!

This is useful when building prompts or combining multiple outputs on a single line inside a script.

Display Variables

echo can display shell variables . In the following example, we are printing the name of the currently logged-in user:

Terminal
echo "Current user: $USER"
output
Current user: linuxize

The shell expands $USER before running echo, so the command prints the value of the current user’s login name.

Inside a script, echo provides a quick way to see what was passed in. Bash stores each argument in a numbered variable, and a few special variables describe the set as a whole:

~/args.shsh
#!/bin/bash

echo "Script name: $0"
echo "First argument: $1"
echo "Argument count: $#"
echo "All arguments: $@"

Run the script with two arguments:

Terminal
bash args.sh alpha beta
output
Script name: args.sh
First argument: alpha
Argument count: 2
All arguments: alpha beta

Quote "$@" whenever you pass the arguments on to another command. The quoted form keeps an argument such as hello world in one piece instead of splitting it into two. Our guides on script arguments and positional parameters cover the rest of the set, including shift and getopts.

Two special variables come up constantly while debugging. $? holds the exit status of the last command that finished, and $$ holds the process ID of the current Bash process. Inside a subshell, $$ still reports the invoking shell’s process ID, while $BASHPID reports the subshell’s process ID.

A status of 0 means success. Here grep finds the string it was looking for:

Terminal
grep -q "root" /etc/passwd
echo "Exit status: $?"
output
Exit status: 0

A nonzero value tells Bash that the command did not succeed, but each command defines the exact meaning of its status codes. For grep, a status of 1 means that no line matched, while 2 indicates an error:

Terminal
grep -q "nosuchuser" /etc/passwd
echo "Exit status: $?"
output
Exit status: 1

Read $? immediately after the command you care about. Every command resets it, and that includes echo itself, so a second read reports the status of the first echo rather than the original command:

Terminal
grep -q "nosuchuser" /etc/passwd
echo "First read: $?"
echo "Second read: $?"
output
First read: 1
Second read: 0

The $$ variable is useful for identifying the shell in logs and diagnostic messages:

Terminal
echo "Shell PID: $$"
output
Shell PID: 4821

Do not use $$ to create temporary filenames. Process IDs are predictable and can be reused, so use mktemp when you need to create a unique temporary file or directory.

Display Command Output

Use the $(command) expression to include command output in the echo argument. The following command displays the current date :

Terminal
echo "The date is: $(date +%D)"
output
The date is: 08/16/26

The command substitution runs date +%D first, then echo prints the returned date as part of the string.

Use Pattern Matching

The shell expands wildcard characters before passing arguments to echo. For example, the following command returns the names of all .php files in the current directory:

Terminal
echo The PHP files are: *.php
output
The PHP files are: index.php contact.php functions.php

The shell expands *.php to matching filenames before echo receives the arguments.

Redirect Output to a File

Instead of displaying the output on the screen, you can redirect it to a file using the > or >> operators:

Terminal
echo "First line" > /tmp/file.txt
echo "Second line" >> /tmp/file.txt

When using >, the file is overwritten. The >> operator appends the output to the file . If the file does not exist, both operators create it.

Use the cat command to verify the contents:

Terminal
cat /tmp/file.txt
output
First line
Second line

The output confirms that the first command created the file and the second command appended a new line to it.

Write to Standard Error

To send output to standard error instead of standard output, redirect file descriptor 1 to 2:

Terminal
echo "Error: something went wrong" >&2

This is useful in scripts where you need to separate error messages from normal output. For more details, see how to redirect stderr to stdout in Bash .

Display Colored Output

Use ANSI escape sequences to change the foreground and background colors or set text properties such as bold and underline:

Terminal
echo -e "\033[1;37mWHITE"
echo -e "\033[0;30mBLACK"
echo -e "\033[0;34mBLUE"
echo -e "\033[0;32mGREEN"
echo -e "\033[0;36mCYAN"
echo -e "\033[0;31mRED"
echo -e "\033[0;35mPURPLE"
echo -e "\033[0;33mYELLOW"
echo -e "\033[1;30mGRAY"
Echo command color output in a Linux terminal

Common Pitfalls

echo looks simple, and most of the time it is. A few behaviors still catch people out once the text stops being a fixed string.

Unquoted variables lose their spacing. The shell splits an unquoted expansion on whitespace and expands any wildcard characters inside it, so the value you stored is not always the value you print:

Terminal
spaced="a   b"
echo $spaced
echo "$spaced"
output
a b
a   b

The first command collapsed three spaces into one. Quote the variable unless you deliberately want that splitting.

A value that starts with a dash disappears. When an argument consists entirely of valid option characters, echo reads it as an option rather than as text. A variable holding -n prints nothing at all:

Terminal
flag="-n"
echo "$flag"
echo "still here"
output
still here

The options are not portable. Bash supports -n and -e, but POSIX does not define either option for echo. When the first argument is -n, different sh implementations may suppress the newline or print -n as text:

Terminal
sh -c 'echo -n hi'

Depending on the shell, this command prints hi without a trailing newline or prints -n hi as a regular line.

When the text is arbitrary, or the script has to run under more than one shell, reach for printf , which treats everything after the format string as data:

Terminal
printf '%s\n' "-n"
output
-n

Quick Reference

For a printable quick reference, see the Bash cheatsheet .

TaskCommand
Print a stringecho "text"
Print without trailing newlineecho -n "text"
Print with escape sequencesecho -e "line1\nline2"
Print a variableecho "$VAR"
Inspect script arguments safelyprintf '<%s>\n' "$@"
Print the last exit statusecho "$?"
Print command outputecho "$(command)"
Write to a file (overwrite)echo "text" > file.txt
Append to a fileecho "text" >> file.txt
Write to stderrecho "error" >&2

FAQ

What does the echo command do in Linux?
The echo command prints its arguments to standard output. It is most commonly used in shell scripts to display messages, show the value of a variable, or write a line of text to a file. By default, echo also adds a newline at the end of the output.

What is the difference between echo and printf?
echo automatically adds a trailing newline and has limited formatting options. printf supports format specifiers (like %s, %d) and does not add a newline unless you include \n. Use printf when you need precise control over output formatting.

How do I print a newline with echo?
Use the -e option with the \n escape sequence: echo -e "line1\nline2". Without -e, the backslash sequence is printed literally.

How do I echo without a newline?
Use the -n option: echo -n "text". This suppresses the trailing newline character.

How do I print the exit status of the last command?
Use echo "$?" directly after the command you want to check. A 0 means the command succeeded, while a nonzero value indicates an unsuccessful result to Bash. Check the command’s documentation for the exact meaning of each status. Read it immediately, because every command that follows overwrites the value, including echo.

Why does echo behave differently on macOS?
echo options and escape handling are not fully portable across shells and systems. Use printf when you need consistent behavior across Linux, macOS, and other Unix-like systems.

Conclusion

The echo command prints text to the terminal and is one of the most commonly used commands in Bash scripts. Use -n to suppress the trailing newline and -e to interpret escape sequences. For more advanced formatting, consider using printf .

In scripts that need predictable formatting, use echo for simple messages and printf when spacing, newlines, or escape handling must be exact.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page