Is `exit $?` Substantially different from `exit` in bash?

I understand that in bash a simple one exitwill end the script with the exit status of the last command. But I also saw people using exit $?, and was interrogated when I suggested that it has the same behavior.

Is there a significant difference between the two scripts?

#!/bin/bash
foo
bar
exit 

and

#!/bin/bash
foo
bar
exit $?
+4
source share
1 answer

There is no difference. When exitcalled without a parameter, it returns the exit code of the last command.

GNU bash. , last_command_exit_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 }
+8

Source: https://habr.com/ru/post/1613064/


All Articles