如果" echo $?"命令返回2?

时间:2016-10-25 06:50:30

标签: linux shell echo

我知道,要看到我们上次执行的命令是正确的是shell,我们使用" echo $?"命令。 为什么它返回2作为输出,它意味着什么?

2 个答案:

答案 0 :(得分:5)

你应该在echo $?之前关注最后一个命令。打开那个命令手册页。你可以找到退出代码的含义。

例如;

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.)
....

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).

man diff

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

答案 1 :(得分:2)

它只显示最后一个命令的错误代码。请检查此链接:Linux System Errors

#define ENOENT           2      /* No such file or directory */