$
Lernen Linux-Handbuch
de
XP:0
Quest 104Pipeline-Logician
+260 XP

Exit Codes & Logical Chaining ($?, &&, ||)

Interaktives Terminal für diese Quest starten→Keine Installation erforderlich • Läuft im Browser

Konzept & Übersicht

Every command executed in Linux returns an exit status code between 0 and 255 upon completion:

- 0: Success (no errors occurred).
- 1-255: Failure or error code.
- The special shell parameter $? stores the exit code of the immediately preceding command: echo $?.

Shell logic operators chain commands based on exit codes:
- cmd1 && cmd2: Logical AND. Runs cmd2 only if cmd1 succeeds (exit code 0).
- cmd1 || cmd2: Logical OR. Runs cmd2 only if cmd1 fails (non-zero exit code).

Combining them creates clean resilient commands: mkdir backup && cp app.db backup/ || echo "Backup failed".

Check the exit code of a successful command by running whoami followed by echo $?. Next, use conditional chaining to ensure the directory exists before copying: run mkdir backup && cp app.db backup/ || echo 'Backup failed'.

Befehlssyntax-Referenz

Command / SyntaxDescription
echo $?Print exit code of the last command
mkdir backup && cp app.db backup/ || echo 'Backup failed'Conditionally create directory, copy file, or report failure

Prüfungsziele

  • ○
    Inspect last exit code with "echo $?"
  • ○
    Chain backup with "mkdir backup && cp app.db backup/ || echo 'Backup failed'"
  • ○
    Verify app.db copied into backup/

Bereit für Quest 104?