Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“exit$?”与bash中的“exit”有什么不同吗?_Bash_Shell - Fatal编程技术网

“exit$?”与bash中的“exit”有什么不同吗?

“exit$?”与bash中的“exit”有什么不同吗?,bash,shell,Bash,Shell,我的理解是,在bash中,一个普通的exit将使用最后一个命令的exit状态完成一个脚本。但我也见过有人使用exit$?,当我建议它有同样的行为时,有人问我 这两个脚本之间有什么有意义的区别吗 #!/bin/bash foo bar exit 及 没有区别。如果在没有参数的情况下调用exit,它将返回最后一个命令的退出代码 下面是GNUBash的代码。如果没有给出参数,它将返回last\u command\u exit\u value,否则它将接受传入的参数,确保它是一个数字,去掉8以外的任

我的理解是,在bash中,一个普通的
exit
将使用最后一个命令的exit状态完成一个脚本。但我也见过有人使用
exit$?
,当我建议它有同样的行为时,有人问我

这两个脚本之间有什么有意义的区别吗

#!/bin/bash
foo
bar
exit 


没有区别。如果在没有参数的情况下调用
exit
,它将返回最后一个命令的退出代码

下面是GNUBash的代码。如果没有给出参数,它将返回
last\u command\u exit\u value
,否则它将接受传入的参数,确保它是一个数字,去掉8以外的任何位并返回该值

  486 get_exitstat (list)
  487      WORD_LIST *list;
  488 {
  489   int status;
  490   intmax_t sval;
  491   char *arg;
  492 
  493   if (list && list->word && ISOPTION (list->word->word, '-'))
  494     list = list->next;
  495 
  496   if (list == 0)
  497     return (last_command_exit_value);      
  498 
  499   arg = list->word->word;
  500   if (arg == 0 || legal_number (arg, &sval) == 0)
  501     {
  502       sh_neednumarg (list->word->word ? list->word->word : "`'");
  503       return EX_BADUSAGE;
  504     }
  505   no_args (list->next);
  506 
  507   status = sval & 255;
  508   return status;
  509 }

看见“裸
退出
的等价物是
exit$?
或者干脆省略退出。”否;您可以省略显式的
exit
,它基本上还是一样的。
  486 get_exitstat (list)
  487      WORD_LIST *list;
  488 {
  489   int status;
  490   intmax_t sval;
  491   char *arg;
  492 
  493   if (list && list->word && ISOPTION (list->word->word, '-'))
  494     list = list->next;
  495 
  496   if (list == 0)
  497     return (last_command_exit_value);      
  498 
  499   arg = list->word->word;
  500   if (arg == 0 || legal_number (arg, &sval) == 0)
  501     {
  502       sh_neednumarg (list->word->word ? list->word->word : "`'");
  503       return EX_BADUSAGE;
  504     }
  505   no_args (list->next);
  506 
  507   status = sval & 255;
  508   return status;
  509 }