2

Suppose I want a bash command to do something extra. As a simple example, imagine I just want it to echo "123" before running.

One simply way to do this would be to alias the command. Since we still need the original, we can just refer to it by its exact path, which we can find using which. For example:

$ which rm
/bin/rm
$ echo "alias rm='echo 123 && /bin/rm'" >> .bashrc

This was easy because I was able to look up the path to rm using which.

However, I am trying to do this with exit, and which doesn't seem to know anything about it.

$ which exit
$ echo $?
1

The command did not output a path, and in fact it returned a non-zero exit code, which which does when a command is not in $PATH.

I thought maybe it's a function, but apparently that's not the case either:

$ typeset -F | grep exit
$ echo $?
1

So the exit command is not defined anywhere as a function or as a command in $PATH, and yet, when I type exit, it closes the terminal. So it clearly is defined somewhere but I can't figure out where.

Where is it defined, and how can I call to it explicitly?

3 Answers3

6

exit is a shell special built-in command. It was built with the shell interpreter, the shell knows about it and can execute it directly without searching anywhere.

On most shells, you can use:

$ type exit
exit is a shell builtin

You have to read source of the shell to see how its builtin implemented, here is link to source of bash exit builtin.

With bash, zsh, ksh93, mksh, pdksh, to invoke the exit built-in explicitly, use builtin builtin command:

builtin exit

See How to invoke a shell built-in explicitly? for more details.

cuonglm
  • 153,898
  • @IsaacBetesh: I'm not sure what I missed. What do you mean explicitly? Please feel free to make an editing. – cuonglm Mar 10 '16 at 03:13
  • My answer has covered all the information, I still don't know. If you think that, then your answer should be marked as duplicated. – cuonglm Mar 10 '16 at 03:29
  • @cuonglm Isaac's question has two parts, "Where is it defined?" and "How can I refer to it explicitly?"; you've covered the first, I'll edit your answer to cover the second... – Stephen Kitt Mar 10 '16 at 08:19
  • @StephenKitt: Ah, if OP changed refer to call, I could get it easier. My bad English. – cuonglm Mar 10 '16 at 08:23
  • Changed it to "call". Not sure why that is more clear. – Isaac Betesh Mar 10 '16 at 14:25
1

exit is a command of a shell so any shell - such as bash or ksh have this command

  • It does have the command. I just want to know where it's defined. I edited the question for clarity. – Isaac Betesh Mar 10 '16 at 02:44
  • As it was answered more fully upper - it's builtin command If you need to overwrite - check official FAQ http://tiswww.case.edu/php/chet/bash/FAQ , G2 – Lev Bystritskiy Mar 10 '16 at 02:57
1

The command exit belongs to the internals of bash. You can use this to find some help:

$ help exit

That just show that exit is known to the shell, but: what is it?:

$ type -a exit
exit is a shell builtin

It will not be found as an external file or program, it does not exist outside the shell.

run this: LESS=+/'^ *exit \[n\]' man bash to find this:

exit [n] Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.

It is also defined as a POSIX builtin.

To call it explicitly in code, use this:

builtin exit