zeewagon zeewagon - 1 month ago 7
Bash Question

What if "echo $?" command returns 2?

I know that, to see our last executed command is correct is shell, we use "

echo $?
" command.
And why it is returning 2 as output and what does it means?

Answer

You should focus last command before echo $?.open that command man page. you could find the meaning of exit code.

For example;

man grep;
...
EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found.  
If an error occurred the exit status is 2.  
(Note: POSIX error handling code should check for '2' or greater.)
....

or

man ls;

   Exit status:
       0      if OK,

       1      if minor problems (e.g., cannot access subdirectory),

       2      if serious trouble (e.g., cannot access command-line argument).

or

man diff

 Exit status is 0 if inputs are
       the same, 1 if different, 2 if trouble.
Comments