Windows
Linux
Terminal & Shell Scripting
Terminal
Windows %errorlevel% equivalent in Linux
Reads the exit status of the last command. 0 means success; any non-zero value means failure.
Windows
→
%errorlevel%
Linux equivalents
$?
$?
$?
Overview
What this command does
Reads the exit status of the last command. 0 means success; any non-zero value means failure.
Migration tip:
$? holds the exit code of the last command. Use && to chain commands that only run on success, and || for failure. set -e makes a script exit immediately on any error.Practical tasks
Common use cases
Check command success or failure exit code
Reads the exit status of the last command. 0 means success; any non-zero value means failure.
$?
Exit code of last command (0=success)
cmd1 && cmd2
Run cmd2 only if cmd1 succeeded
cmd1 || cmd2
Run cmd2 only if cmd1 failed
Ready to run
Copyable Linux commands
Review paths, device names, package names and permissions before running any command.
ls /tmp
echo $?
ls /nonexistent
apt update && apt upgrade -y
Walkthrough
Examples with explanations
# Windows: if %errorlevel% neq 0 echo Error
ls /tmp
echo $? # 0 (success)
ls /nonexistent
echo $? # 2 (error)
# Chain commands:
apt update && apt upgrade -y
Reference
Common options and variations
| Command or option | Use |
|---|---|
$? | Exit code of last command (0=success) |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeded |
cmd1 || cmd2 | Run cmd2 only if cmd1 failed |
set -e | Exit script on first error |
set -o pipefail | Catch errors inside pipes |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.
| Distribution | Package manager / base | Equivalent command | Difference to notice |
|---|---|---|---|
| Debian/Ubuntu | same command | $? |
Same command on this distribution. |
| Fedora | same command | $? |
Same command on this distribution. |
| Arch | same command | $? |
Same command on this distribution. |
Keep learning