Check Status and Find Local Help
Check Status and Find Local Help
Not every command gives you a friendly success message. A file check may print nothing whether it succeeds or fails. Alongside its visible output, a command returns an exit status: a small number that the shell can inspect. This gives you a second way to understand the result.
Zero commonly means success. Other numbers need to be interpreted using that command's rules: for example, a comparison tool can use a nonzero status to report a difference rather than a broken operation. Let's learn how to capture the status first, then where to look up what the command means by it.
What you’ll learn
- Explain the operation and choose it for the stated school task.
- Read the command arguments and check the result rather than relying on silence alone.
- Apply the worked task and recognize this boundary: Read $? immediately because the next command replaces it. man, help and --help availability depends on the utility and installed documentation; Bash help covers its builtins.
A result has both output and a status
Check
test examines whether the specified file exists.
test -e missing.txtCapture
The failed check returns 1; save it immediately.
status=$?Report
printf can now display the saved value without losing it.
Status: 1
Save the status of a missing-file check
Prepare the example
Start in a fresh practice directory where missing.txt does not exist. We are deliberately checking an absent file. The check is not supposed to succeed, and it does not change the filesystem. Run the lines in order so the status belongs to the operation we are discussing.
Bash · Linux terminal
test -e missing.txt
status=$?
printf 'Status: %s\n' "$status"Expected result
Status: 1test -e missing.txt asks whether that path exists. The check is false, so it returns status 1 without printing a description. The special shell value $? contains the most recent command's status. status=$? saves it immediately into an ordinary variable.
The final printf uses %s as a place for that saved value and prints Status: 1. Saving the value matters: another command, even an innocent message, would replace $? with its own status. When you need more detail, consult the utility's manual or documented help option; Bash's help covers its built-in commands, while man depends on installed manual pages.
Try it, then check the worked answer
Your task: When should you save $? for a command you want to inspect?
Start in a fresh practice directory where missing.txt does not exist. We are deliberately checking an absent file. The check is not supposed to succeed, and it does not change the filesystem. Run the lines in order so the status belongs to the operation we are discussing.
Bash · complete example for this question
test -e missing.txt
status=$?
printf 'Status: %s\n' "$status"Expected result
Status: 1Worked answer
Immediately after that command.
Capture $? immediately after the operation whose result you want. Once a later command finishes, the shell has a newer result. Keeping the old status in a named variable lets you print messages or perform other work without accidentally reporting the status of that later work.
Check your decision
Save the status before another command replaces it
Visible output and exit status answer different questions. Inspect both when diagnosing a command. Do not assume every nonzero number has the same meaning across tools, and do not wait until several commands later to read $?. Local help is especially useful when installed versions support different options.
Reaching the recap and completing the quizzes are separate milestones.