Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

echo

Print text or the value of variables to the screen (or to a file with redirection).

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash (built-in) / coreutils

What it does

echo writes its arguments, separated by spaces, followed by a new line. It is the simplest way to show a variable, write a line to a file, or print a message in a script.

Bash has echo built in; there is also a program /usr/bin/echo from coreutils. They work the same for normal use.

For exact formatting (numbers, tabs, no surprises with backslashes), printf is the safer tool.

How to write it

echo [-neE] [TEXT ...]

Options 13

Most used

-n
Do not print the new line at the end.
-e
Read backslash codes like \n (new line) and \t (tab).
-E
Do not read backslash codes (the default).

Backslash codes (with -e)

\n
New line.
\t
Tab.
\\
A backslash.
\a
Alert (beep).
\b
Backspace.
\c
Stop printing here (nothing more is printed).
\e
Escape character (start of color codes).
\r
Carriage return (go to the start of the line).
\0NNN
The character with octal value NNN.
\xHH
The character with hex value HH.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Print a message

    echo "Backup started"
    Backup started

    Quotes keep the spaces as they are.

  2. 2Show the value of a variable

    echo "$HOME"
    /home/student

    Always put variables in double quotes, so spaces in the value do not break the line.

  3. 3Write a line to a file

    echo "192.168.122.20  server2.example.com server2" >> /etc/hosts

    >> adds the line at the end. With > the file would be emptied first.

  4. 4Write to a file that needs root, from a normal user

    echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/90-forward.conf

    sudo echo ... > file does not work, because the redirect runs as your user. tee runs with sudo.

  5. 5Print several lines with -e

    echo -e "line one\nline two\ttab"

    \n makes a new line and \t a tab.

  6. 6Print without a new line (for a prompt)

    echo -n "Enter the server name: "; read SERVER

    The cursor stays on the same line.

  7. 7Show the exit code of the last command

    grep -q root /etc/passwd; echo $?
    0

    0 means success. Any other number means an error.

  8. 8Print colored text in a script

    echo -e "\e[31mERROR\e[0m disk is full"

    \e[31m = red, \e[0m = back to normal. Good for warnings in deploy scripts.

  9. 9Make a quick test file

    echo "hello" > /tmp/test.txt

    Useful to test permissions, SELinux labels or a web server.

  10. 10Show the PATH, one folder per line

    echo "$PATH" | tr ':' '\n'

    Easier to read than one long line.

  11. 11Check a variable inside a script while you debug

    echo "DEBUG: user=$USER host=$(hostname) dir=$PWD" >&2

    >&2 sends the message to the error channel, so it does not mix with the normal output.

  12. 12Use brace expansion to see what it makes

    echo web{01..03}.example.com
    web01.example.com web02.example.com web03.example.com

    Bash expands the braces before echo runs. Good to test a pattern before you use it.

Watch out

  • sudo echo text > /root/file fails: the redirect is done by your shell, not by sudo. Use | sudo tee.
  • Text that starts with -n or -e is read as an option. Use printf '%s\n' "$TEXT" for unknown text.
  • Without quotes, several spaces become one and * is expanded to file names.

Tips to remember

  • echo $? = did the last command work?
  • > = new file, >> = add to the file.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man echo