Ingo Schwarze wrote:
> su(I) goes back all the way to v1: (...) It wasn't in v0 PDP-7 UNIX though:
> https://www.tuhs.org/Archive/Distributions/Research/McIlroy_v0/UnixEditionZero.txt

>From that text file:
>> 8.4 chdir -- change directories
>>
>> To change the current directory, use
>>
>>       chdir dirname
>>
>> This command is the only one that does not reside in directory
>> "/bin"; instead it is part of the Shell. The reason is
>> interesting. Recall that each ordinary command is executed as a
>> separate process created by the Shell. If the system’s chdir
>> primitive were executed in such a process, it would have essentially
>> no effect, since the process would terminate instantly
>> without affecting the current directory of the Shell process and
>> its subsequent offSpring.[sic] The Shell itself recognizes the chdir
>> command and calls the system to change directories without
>> creating a new process.

Whoa.

This probably isn't news to most people here, but that just blew my
mind a little.
Please CORRECT ME if any of the following is still wrong:

I've--embarrassingly--only realised now that the shell is a decidedly
NON-special program in terms of what it does when the cd (formerly
chdir) command is issued at its prompt. Keeping state in remembering
your current working directory isn't so much something the shell does
for, to and on behalf of users and programs, it's actually something
the shell does TO ITSELF, by simply calling the chdir(2) syscall like
any other program would. The shell doesn't chaperone or nanny *you*,
it transplants *itself*. Effectively, the shell then runs *in* the new
current directory, and that's why the question whether to cd or not to
cd into a directory is controlled by execute permissions.

Much elegant.
Very Unix.
Wow.

This is made slightly clearer by the language used in the POSIX spec
and on a bunch of other, non-OpenBSD man pages derived from it:

>> The cd utility shall change the working directory of the current shell 
>> execution environment...
<http://pubs.opengroup.org/onlinepubs/9699919799.2013edition/utilities/cd.html>

If that's all correct, then would it make sense to retrofit any of the
above enlightening language (from 8.4 or POSIX) into OpenBSD's sh or
ksh man pages? I don't suppose a separate cd(1) man page makes sense?
(There's cd(4), but that's different.)

Ian

Reply via email to