Live data from Hacker News

How does `cd` work?

blog.safia.rocks

51–60 of 93 posts

Re: How does `cd` work?

#52
post #28
post #21

Not 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.

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 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?

#53
post #41
post #39

On 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.

Man or woman, the feedback seems to apply the same way.

Re: How does `cd` work?

#54
post #39

On 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.

I will speculate that it's because the author mentions "Julia Evans" [1] who is quite famous in this community for their articles. That or more people than I thought didn't know some details about how basic commands/built-in functions work in Unix. Nevertheless, whatever people find interesting, they upvote. Good or not, this article caught the attention of many, that's all.

[1] https://twitter.com/b0rk

Re: How does `cd` work?

#55
Not 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?

#56
post #39

On 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 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?

#57
post #6

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 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 :)

[1] https://www.bell-labs.com/usr/dmr/www/hist.html

Re: How does `cd` work?

#58
post #39

On 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 agree. In the post about ls, I expected to read about how the program builds the list of files and directories, which system calls are involved and so on. The author doesn't seem to like reading C source code and that's fine. Studying the strace of a simple ls invocation and cross-referencing with the Linux man pages would have revealed the inner workings of the program, though.

>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?

#59
post #6

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…

[deleted]

Re: How does `cd` work?

#60
post #39

On 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…

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 treatments.
Post reply on HN