As a historical curiosity, in the very first versions of Unix chdir was a normal command rather than a shell builtin. You see, at that point Unix had no fork system call. There were multiple processes, but they were created statically at startup rather than on-demand. Running a command in the shell would cause the command to replace the shell in the address space of the process, and the process quitting would put the…
To elaborate, I really enjoyed reading Dennis Ritchie give some background on the evolution of Unix[1]: > As a historical curiosity, in the very first versions of Unix chdir was a normal command rather than a shell builtin. > You see, at that point Unix had no fork system call. There were multiple processes, but they were created statically at startup rather than on-demand. Running a command in the shell would cause…
How does `cd` work?
61–70 of 93 posts
Re: How does `cd` work?
#62Would the title "How does 'cd' work in MacOS?" be better for readers wishing to learn, or worse? I wonder.
The blog posts from her friend at jvns.ca have been similarly Linux-centric but never use titles that inform the reader as such, i.e., "How does X work?" versus "How does X work in Linux?"
It makes me ask, how important is it for students to be aware of the Holy Grail of UNIX: portability.
How long should one remain blissfully ignorant of incompatibilities between UNIX-like OS that work against those "still seeking the Holy Grail"?
As a user, the UNIX programmers I admire the most are the ones who understand the value of portability, set it as a goal and through broad knowledge and utmost care can get very close to achieving it, despite the mind-numbing work or the tradeoffs this might entail.
Re: How does `cd` work?
#63The funny thing here: why does cperciva, of tarsnap fame, show up in the source code of the CD script? I saw it on the article, checked it on my Mac, and it does indeed show up. Intriguing.
Mac's userland is based on FreeBSD. Colin Percival is a FreeBSD developer who last modified this file: https://github.com/freebsd/freebsd/commit/0bc1bed704cc7b7292...
Re: How does `cd` work?
#64Earlier quoted context omitted.
Also some shells will refuse to run a builtin if there is no executable in the path that matches it. POSIX actually mandates this behavior for any builtin not on a specific list, though most shells (even dash, which is typically obsessive about complying with POSIX) do not implement this behavior.
That didn't sound right to me, but I looked it up, and you're right. The scripts we're discussing were written in 2002 [1], so the time-appropriate version is POSIX-2001 (Issue 6). [1]: https://github.com/freebsd/freebsd/commit/55d0b8395514ae4055... But you don't have to take my word for it: I've paste-bin'ed the entirety of what POSIX-2001 had to say about shell built-ins (in general, I didn't include man-pages for…
Re: How does `cd` work?
#65Earlier quoted context omitted.
Or try zsh with a good pre-made config like grmlzshrc[1]. It allows changing to a directory by typing only its name (or path). This includes the .. directory. Doesn't get faster. [1] https://grml.org/zsh/
Or use Autojump[1], changed my life 1. https://github.com/wting/autojump
Re: How does `cd` work?
#66As a historical curiosity, in the very first versions of Unix chdir was a normal command rather than a shell builtin. You see, at that point Unix had no fork system call. There were multiple processes, but they were created statically at startup rather than on-demand. Running a command in the shell would cause the command to replace the shell in the address space of the process, and the process quitting would put the…
The pattern of development:
- Make A
- Make B better
- A now broken because it had dependencies on B
- (maybe) Realize a fundamental pattern that unifies A and B.
- (maybe) Unify principles so that A and B become lemmas.
This is a very scientific way of working. The truth emerges as the byproduct of keeping all the plates spinning in your model. As long as you remain aware of all the plates, you'll eventually realize something deep. It's also the strongest argument I can think of in favour of rewriting a codebase plan-9 style. Once you have the truth in mind, bake your knowledge for big wins. Baked knowledge can form the foundation upon which you can climb higher.But many developers don't keep all the plates spinning. If you don't have everything (or some subset) making sense in some unified theory, don't rewrite. Without a unified grand theory, then you can't make something better. This is why I write unit tests — doing so keeps your plates spinning. It's not about proving your language and libraries are doing what is expected (although that's valuable too in some languages without unified libraries) — it's about showing that everything is working together the way your grand theory intends. Unit tests should be written to reject some part of the null hypothesis.
Re: How does `cd` work?
#67Not one character in the article is actually devoted to how `cd` works...it's just the story of how the author found out where to find the code for it. An article much more worthy of the title "How does `cd` work?" would maybe, you know, actually go through the code that makes `cd` work.
Re: How does `cd` work?
#68I know it's a low-level system call in most on-disk operating systems to change directories. But, for instance, how does it translate physical address to the human-readable name? Do modern (WinXP+) implementations actually take the time to translate folder names from 8.3 to the extended name field? How?
Heck, I'll be dumb enough to ask - why, specifically at the call level, does 'cd\' stick to one level up/down whereas 'cd ' can just pull from just about anywhere? And why, dare I ask, can 'cd' not display like 'tree'?
These are questions about how 'cd' works, to me. Not just, oh, in (foo)Nix it's a system call.
Re: How does `cd` work?
#69> The Bash shell invokes the chdir function.
Nope! Not quite.
Bash:
~$ ls -ld foo
lrwxrwxrwx 1 kaz kaz 4 Mar 1 6:50 foo -> /etc
~$ cd foo
~/foo$ pwd
/home/kaz/foo
~/foo$ cd ..
~$ pwd
/home/kaz
Raw chdir and getcwd syscalls: $ pwd
/home/kaz
$ txr
This is the TXR Lisp interactive listener of TXR 190.
Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
1> (chdir "foo")
t
2> (pwd)
"/etc"
3> (chdir "..")
t
4> (pwd)
"/"
See the difference? While of course the shell will invoke chdir, it has its own idea of a current working directory, and translates the argument of cd to something else. For instance cd .. doesn't translate to (chdir "..").Re: How does `cd` work?
#70Earlier quoted context omitted.
Everybody who comes to know about systems calls will have learned the concept for the first time. Some people come to that knowledge by becoming a web developer, gaining some rudimentary understanding of the command line, typing `which cd`, and eventually reading the bash source. The fact that this is even possible is a testament to the author's curiosity and the value of free software. Others come to that knowledge…
I think gp is not ragging on the blog's author for being a novice at Unix systems internals but lamenting the " I looked into this but then it all seemed so complicated so I stopped" attitude that is present in this as well as the sibling posts on sudo and ls. I would much prefer to read three posts delving deeper into the inner workings of any one of these commands than the existing three muddled surface level treat…
Coming to it now, with all the layers that have built up -- the side-track for the script that uses tr, and the idiom with ${1+"$@"}, and all the cruft build up within sh(1) -- it must be pretty hard to separate what is incidental from what is fundamental.