https://www.amazon.com/Advanced-Programming-UNIX-Environment...
How does `cd` work?
51–60 of 93 posts
Re: How does `cd` work?
#52Not to be negative, but for learning: There are a few "problems" with this snippet of the article: ---- $ which cd /usr/bin/cd $ cat /usr/bin/cd #!/bin/sh # $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $ # This file is in the public domain. builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"} Oh, bother! Reading shell scripts can be such a hassle sometimes. I know the tr comma…
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.
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 individual built-ins): https://lukeshu.com/dump/posix-2001-builtins.txt
As for whether that's still true today, looking at POSIX-2008 (Issue 7), 2013 edition (I don't have a copy of the 2016 edition handy), none of that has changed.
Re: How does `cd` work?
#53On the one hand, I admire the search for knowledge, taking things apart and seeing how they work. On the other hand, this seems a bit cargo-cultish. It's as if I took apart a record player, trying to see how it plays music, and I told you it works because the motor turns the the record. In this case, the author got tangled in the shell script and code, and totally missed the whole subtlety and complexity of the Unix…
Author is a she by the way.
Re: How does `cd` work?
#54On the one hand, I admire the search for knowledge, taking things apart and seeing how they work. On the other hand, this seems a bit cargo-cultish. It's as if I took apart a record player, trying to see how it plays music, and I told you it works because the motor turns the the record. In this case, the author got tangled in the shell script and code, and totally missed the whole subtlety and complexity of the Unix…
I had the same sentiment as yours, but the blog is fine (for the author and appropriate audience). I am quite surprised/curious how it got to top page here.
Re: How does `cd` work?
#55An 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?
#56On the one hand, I admire the search for knowledge, taking things apart and seeing how they work. On the other hand, this seems a bit cargo-cultish. It's as if I took apart a record player, trying to see how it plays music, and I told you it works because the motor turns the the record. In this case, the author got tangled in the shell script and code, and totally missed the whole subtlety and complexity of the Unix…
Others come to that knowledge by reading a 650 page book about System V after graduating from university. Maybe they already learned it before getting the book, in some practical circumstance, like the author. Maybe they have always known it. But for me, an article by someone in the midst of their learning is a great help, an opportunity for others to share what they know, and exemplary of the hacker spirit.
Re: How does `cd` work?
#57As 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…
> 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 shell back in there.
> This worked perfectly with cd being a normal command. Then they implemented fork() and were for a while very confused trying to debug how in the world fork() could have broken the chdir() system call :)
Re: How does `cd` work?
#58On the one hand, I admire the search for knowledge, taking things apart and seeing how they work. On the other hand, this seems a bit cargo-cultish. It's as if I took apart a record player, trying to see how it plays music, and I told you it works because the motor turns the the record. In this case, the author got tangled in the shell script and code, and totally missed the whole subtlety and complexity of the Unix…
>It’s largely the kind of stuff that higher level languages implement in their standard library
I think it's extremely interesting. It's in these libraries that the hidden fun stuff happens. For example, memory allocation and related terms like the heap seem like magic but it becomes clearer once one learns about how it works.
Re: How does `cd` work?
#59As 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…
Re: How does `cd` work?
#60On the one hand, I admire the search for knowledge, taking things apart and seeing how they work. On the other hand, this seems a bit cargo-cultish. It's as if I took apart a record player, trying to see how it plays music, and I told you it works because the motor turns the the record. In this case, the author got tangled in the shell script and code, and totally missed the whole subtlety and complexity of the Unix…
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…