I've always wondered why cd isn't a program, but never managed to find the answer.
Anyone know why this is the case?
|
The "current working directory" is a property that is unique to each process. So, if
|
|||||
|
|
A builtin cd is mandatory as of course the external implementation doesn't change the current shell directory. However, the latter can still be useful. Here is an example showing how POSIX envision this
It will report an error message for all directories you aren't allowed to And here is the answer to your question by one of the Unix original co-author. On a very early Unix implementation, Quoting Dennis Ritchie:
Source: Dennis M. Ritchie, “The Evolution of the Unix Time-sharing System”, AT&T Bell Laboratories Technical Journal 63(6), Part 2, Oct. 1984, pp.1577–93 Unix Version 1 (March 1971) chdir manual page states: Because a new process is created to execute each command, chdir would be ineffective if it were written as a normal command. It is therefore recognized and executed by the Shell. |
|||||||||||||||||||||
|
|
From the Bash introduction (What is a shell?):
|
||||
|
|
|
For April Fool's this year, I wrote a standalone version of No one got the joke. Sigh. Anyone who isn't sure that Read its man page, too. :) |
|||||||||||||
|
|
cd is a shell built-in command. As easy as is. The man cd says it all. the cd command changes the working directory for all interpreters and (in a threaded environment) all threads. |
|||||||||||||||||
|
|
I think one thing missing in people answer is that current directory is a environment variable that each program can change. If you use 'export' command to see your current environment variables list, you will have:
in your results. Thus by 'cd' command we just want to modify this internal variable. I think if we try, we can chage the PWD variable of any pty in shell, of course. Like:
But I think there s no need in normal cases. In another word, we take help from bash(or any shell) to modify its internal variable defined. |
|||||||||||||||||
|
cdcommand was a separate program. The shell handled it specially in that it did notfork, justexec. And whencdwas done, it would execsh. I don't know if this is a true story. – camh May 17 '12 at 2:18chdirsyscall. sources: v1 v5 v7 (first version with Bourne shell) – Mikel May 17 '12 at 5:33cdthat I had read. I was clearly wrong about aspect of it, now that @jlliagre has filled in the details. – camh May 17 '12 at 8:23