Suppose I run the command
exit
Obviously the terminal exits as that is the purpose of the command.
I understand sudo means to run the command with administrator privileges. if I run:
sudo exit
Then I get "sudo: exit: command not found" from the terminal. Why doesn't the terminal in this case recognize the command and proceed to exit the terminal as it did when ran without sudo?
Most "commands" in a unix-like environment are in fact external programs and scripts. A few, like exit
, are actual commands built into the shell. sudo
cannot recognize the latter.
From the man page:
When sudo runs a command, it calls fork(2), sets up the execution environment as described above, and calls the execve system call in the child process.
From the execve
man page:
execve() executes the program pointed to by filename.
There is no program named exit
; it's just a command the shell recognizes.
Think about it this way: Unix grants permissions to a process. sudo
runs a process with elevated/modified permissions. But when you run exit
you're not spawning a new process. sudo
can't elevate the permissions of the already-running shell; so what would it possibly do?
What you could do is tell sudo
to spawn another shell, passing exit
as a command to run that shell... which will do nothing, of course.