Windows %errorlevel% equivalent in Linux
Reads the exit status of the last command. 0 means success; any non-zero value means failure.
%errorlevel%
$?
$?
$?
Overview
What this command does
On Windows, "%errorlevel%" is used to check command success or failure exit code. On Linux, the closest practical mapping shown here is "$?". Reads the exit status of the last command. 0 means success; any non-zero value means failure. This reference is grouped under Terminal & Shell Scripting → Terminal.
Windows → Linux context
What changes when you use this on Linux?
Equivalent task, different tool
Windows %errorlevel% maps to $? for this task. Treat this as a workflow mapping rather than a character-for-character syntax translation.
Portable across the compared distros
The Debian/Ubuntu, Fedora and Arch rows use the same command in this mapping. Package availability or defaults can still vary, so check the installed tool version when an option behaves differently.
For the broader concept behind this mapping, read Linux command-line basics.
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
Debian/Ubuntu, Fedora and Arch all use "$?" for this mapping. The command syntax shown here is portable across the three rows, although installed versions and system defaults can still vary.
| 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