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.

  • 3
    I don't think it's ever evaluated.
    – dawud
    Jun 19 '14 at 20:15
  • You can read it yourself: git.savannah.gnu.org/cgit/bash.git/tree/builtins/exit.def
    – dawud
    Jun 19 '14 at 20:19
  • Can you show me in the source code of Bash where exactly it is not evaluated?
    – Ryan
    Jun 19 '14 at 20:19
  • 6
    exit is not an alias for logout
    – goldilocks
    Jun 19 '14 at 20:23
  • 2
    "So the right hand must be evaluated" -- The right hand side gets parsed, not evaluated.
    – phemmer
    Jun 19 '14 at 20:29

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

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

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

  • 2
    @mikeserv You are confused. The arguments to exit are indeed evaluated, but any (separate) commands which come after it are not. exit 5 is a single command; exit 5 && exit 10 is two, and the second will never be executed.
    – amalloy
    Jun 20 '14 at 0:47

It's never executed, because the shell exited. Here is an easy way to test:

$ bash
$ touch /tmp/testfile
$ exit && rm /tmp/testfile
$ ls /tmp/testfile 

Note that I first started a second shell so that my XTerm wouldn't exit. The same result is obtained when I don't do so and check for file existence from a different window.

cmd1 && cmd2 means run cmd1 then, if it's successful (exit code = 0), run cmd2. So, first the shell runs exit. Exiting causes the shell to cease existing, so it never gets to the "if it's successful" part.

Your followup with the syntax error is different: Syntax is checked when the input line is parsed, before any part of it is executed. Basically, bash doesn't understand what you mean at all, so it can't begin to execute it.

  • 5
    Just for some fun: function exit() { echo "where is your god now?"; }; exit && echo eeek
    – phemmer
    Jun 19 '14 at 20:27
  • 1
    @Patrick But Control-D still works! You haven't obtained everlasting shell-life yet.
    – derobert
    Jun 19 '14 at 20:29
  • kill -9 $$ works too, although terminal emulator will complain that the shell crashed (at least Yakuake does).
    – Ruslan
    Jun 20 '14 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.