Sobes.tech
Middle

What variables do you know in bash? The variable for the last command's return code?

sobes.tech AI

Answer from AI

In bash, there are different types of variables:

  • Regular variables — simply store values, for example VAR=123.
  • Environment variables — accessible to child processes, for example export PATH.
  • Special variables — provide information about the shell and command status.

The variable with the exit code of the last executed command is $?. It contains an integer value, where 0 indicates success, and any other number indicates an error.

Example:

ls /nonexistent
echo "Exit code: $?"  # Will output a non-zero exit code

ls /
echo "Exit code: $?"  # Will output 0