Bash – What happens in Bash after calling exit

bash

I understand that when I call exit, it is an alias for logout. Sometimes, just for fun, when I need to remove myself from a session I will type exit && 1. Now what happens after the exit has been executed. Where does the 1 go? Typing 1 into bash yields (obviously) this: 1: command not found. I'm not asking why 1 doesn't work. I'm asking where does the 1 go after calling exit? 1 is just an example, replace it with any other command.

But typing exit &&&&&&& 1 yields a syntax error. So the right hand must be evaluated.

Disclaimer : This is a question in which interests me. There is not particular reason for this question besides the fact that I am curious about what happens.

Best Answer

When you type exit, the shell will quit immediately, 1 is not evaluated. If you check the source code for exit, you can see:

int
exit_builtin (list)
     WORD_LIST *list;
{
  if (interactive)
    {
      fprintf (stderr, login_shell ? _("logout\n") : "exit\n");
      fflush (stderr);
    }

  return (exit_or_logout (list));
}

The last thing exit does: return (exit_or_logout (list))

static int
exit_or_logout (list)
     WORD_LIST *list;
{
  int exit_value;

  ..............

  /* Get return value if present.  This means that you can type
     `logout 5' to a shell, and it returns 5. */

  /* If we're running the exit trap (running_trap == 1, since running_trap
     gets set to SIG+1), and we don't have a argument given to `exit'
     (list == 0), use the exit status we saved before running the trap
     commands (trap_saved_exit_value). */
  exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list);

  bash_logout ();

  last_command_exit_value = exit_value;

  /* Exit the program. */
  jump_to_top_level (EXITPROG);
  /*NOTREACHED*/
}

The syntax error in exit &&&&&&& 1 due to parsing error, not the result of evaluating expression. Parsing occurs before any command run.