Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

echo

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

Niveau
RHCSA-examen
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in) / coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

echo [-neE] [TEXT ...]

Opties 13

Meest gebruikt

-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.

Voorbeelden 12

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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 om te onthouden

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

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man echo