← All commands
Topic hub Terminal & Shell Scripting
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
Debian/Ubuntu same command
$?
Fedora same command
$?
Arch same command
$?

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 optionUse
$?Exit code of last command (0=success)
cmd1 && cmd2Run cmd2 only if cmd1 succeeded
cmd1 || cmd2Run cmd2 only if cmd1 failed
set -eExit script on first error
set -o pipefailCatch 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.

DistributionPackage manager / baseEquivalent commandDifference 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

Related Terminal & Shell Scripting commands