“exit$?”与bash中的“exit”有什么不同吗?
我的理解是,在bash中,一个普通的“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以外的任
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 }