In shell script, "what does mean" refers to the exit status returned by a command-typically 0 for success and non-zero for failure. This value determines whether subsequent commands run and influences script flow.
Every command in a shell script returns an exit code. The shell uses this code to decide whether the previous command succeeded. A code of 0 means success; any nonzero value indicates an error. This behavior is standardized across Unix-like systems, including Linux and macOS.
For example, running ls /tmp returns 0 if the directory exists. If you run ls /nonexistent, it returns 1. The shell script checks this value to determine the next step. This mechanism underpins conditional logic in scripts.
When a command runs, it exits with a status between 0 and 255. The shell stores this value in the special variable $?. You can check it immediately after a command:
ls /tmp
echo $?
# Output: 0 (success)
ls /nonexistent
echo $?
# Output: 2 (file not found)
The value $? is overwritten by each command. Always check it right after the command you're testing.
Scripts use exit codes in conditionals. For example:
if ls /tmp >/dev/null; then
echo "Directory exists"
else
echo "Directory missing"
fi
This script checks the exit status of ls. If it returns 0, the then block runs. If it fails, the else block executes.
While 0 means success, specific non-zero values carry meaning. Here are standard codes:
| Code | Meaning | Example |
|---|---|---|
| 0 | Success | grep "hello" file.txt finds the string |
| 1 | General error | rm nonexistent_file fails to remove |
| 2 | Invalid command usage | mv called with incorrect syntax |
| 126 | Command not executable | Script lacks execute permission |
| 127 | Command not found | unknown_command doesn't exist |
| 128+ | Signal termination | Process killed by signal (e.g., 130 = SIGINT) |
These codes are defined in the POSIX specification. The POSIX standard states that exit codes must be integers in the range 0-255, with 0 indicating success.
Some tools use custom exit codes. For example, git returns 128 for repository errors, 129 for invalid arguments. Always consult the tool's documentation for exact meanings.
Exit codes control script execution flow. Without them, scripts would run blindly. Consider a backup script:
#!/bin/bash
tar -czf backup.tar.gz /home/user >&1
if [ $? -ne 0 ]; then
echo "Backup failed"
exit 1
fi
echo "Backup completed successfully"
If tar fails, the script stops early. This prevents corrupted archives from being moved or archived.
Using set -e makes the script exit immediately on any command failure. This is common in CI/CD pipelines. For example:
#!/bin/bash
set -e
make install
cp config.json /etc/
echo "Deployment complete"
If make install fails, the script stops. No further steps run. This avoids deploying broken software.
Without proper exit code handling, scripts may continue after critical failures. One test script I wrote once ran apt-get update and continued despite network issues. The result was a broken system. Fixing it required checking $? after every command.
Many assume that a command returning 0 means "everything went fine." That's not always true. A command can succeed but still fail in purpose. For example:
grep "error" log.txt
echo $?
# Returns 0 if "error" appears anywhere
But if the log contains no errors, grep returns 1. So a 0 exit code means the string was found-not that the system is healthy.
Another mistake: assuming all failures return 1. Some tools return different codes. ping returns 1 if no response, 2 if it fails to resolve the host. Using if [ $? -eq 0 ] without checking the actual error is unreliable.
Also, exit codes are not messages. They don't tell you *why* something failed. You must read logs or use verbose flags. For example:
ssh user@host "ls /root"
echo $?
# Returns 1 if SSH access denied, but says nothing about the reason
To debug, run with -v or check system logs.
Use test or [ to compare exit codes. For example:
if [ "$?" -eq 0 ]; then
echo "Success"
elif [ "$?" -eq 1 ]; then
echo "General failure"
else
echo "Unknown error"
fi
You can also chain commands using && and ||. These operators depend on exit codes:
command1 && command2 || command3
This runs command2 only if command1 succeeds. If command1 fails, command3 runs.
For complex scripts, define functions that return meaningful exit codes:
validate_config() {
if [ ! -f "$1" ]; then
echo "Config file missing"
return 1
fi
if grep -q "missing_key" "$1"; then
echo "Missing required key"
return 2
fi
return 0
}
validate_config /etc/app.conf
case $? in
0) echo "Valid" ;;
1) echo "File missing" ;;
2) echo "Key missing" ;;
*) echo "Unexpected error" ;;
esac
This function returns different codes based on the issue. The calling script can respond appropriately.
Understanding what does mean in shell script means recognizing that exit codes are the primary way commands communicate success or failure. They are not optional-they are essential for reliable automation.
Always check $? after critical commands. Use set -e to stop on errors. Avoid assuming all failures return 1. And remember: a 0 exit code means the command finished, not that everything is okay.