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

exit

End the shell or script with an exit status (0 = success, 1–255 = error); return does the same for a function.

Niveau
Dagelijks Linux
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in)
Ook
return

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

Wat het doet

Every command returns an exit status: 0 means success, any other number an error. $? holds the status of the last command. Scripts should end with exit 0 on success and a non-zero code for errors, so that other scripts, cron and systemd know what happened. exit in an interactive shell logs you out.

Zo schrijf je het

exit [N]
return [N]

Belangrijkste delen

0
Success.
1
General error.
2
Wrong use (often from builtins).
126
Found, but cannot run.
127
Command not found.
128+N
Killed by signal N (130 = Ctrl+C, 137 = kill -9).

Opties 1

Meest gebruikt

N
The exit status (0–255; default: the status of the last command).

Voorbeelden 10

$ als gewone gebruiker# als root (of met sudo)

  1. 1End a script with success

    tail -1 backup.sh
    exit 0

    The caller sees 0.

  2. 2Exit with an error

    [ -d /backup ] || { echo "no backup folder" >&2; exit 1; }

    Message to stderr, code 1.

  3. 3See the status of the last command

    ls /nothing; echo $?
    ls: cannot access '/nothing': No such file or directory
    2

    ls uses 2 for this error.

  4. 4Command not found

    nosuchcommand; echo $?
    127

    127 = not found.

  5. 5Killed by Ctrl+C

    sleep 100
    ^C
    echo $?
    130

    128 + 2 (SIGINT).

  6. 6Return from a function

    isroot() { [ "$(id -u)" -eq 0 ] && return 0 || return 1; }; isroot && echo root || echo "not root"

    return = exit for functions.

  7. 7Use the status in if

    if grep -q lisa /etc/passwd; then echo found; fi

    if checks the exit status.

  8. 8Pass on the status of the last command

    cp a b; exit

    exit without N uses the status of cp.

  9. 9Leave a root shell

    exit

    Back to your own user.

  10. 10Exit codes in systemd

    systemctl status myapp | grep -i 'code='

    status=203/EXEC = the program could not be run.

Exitcodes

N :: The given value (or the last command's status).