Junior — Middle
How to determine the exit code of a running command in Linux?
sobes.tech AI
Answer from AI
In Linux, the exit code of the last executed command is stored in a special environment variable $?.
To find out the exit code, simply execute after the command:
echo $?
A code of 0 usually indicates successful execution, while any other value signifies an error or a special condition.
Example:
ls /nonexistent
echo $? # Will output the error code, for example 2
This allows scripts and users to check the result of command execution.