Live data from Hacker News

How does `cd` work?

blog.safia.rocks

1–10 of 93 posts

Re: How does `cd` work?

#3
The 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.

Re: How does `cd` work?

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

#7
post #3

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

#8
I tried to create a command line tool that mimics some behavior of a bookmark manager in the terminal.

https://github.com/serv/lbm

Unfortunately, I learned that there's is no way to invoke cd programmatically from a program, but I didn't get an explanation I could understand why this is not possible.

Can someone explain why you can't invoke cd from a program?

Re: How does `cd` work?

#9
post #8

I tried to create a command line tool that mimics some behavior of a bookmark manager in the terminal. https://github.com/serv/lbm Unfortunately, I learned that there's is no way to invoke cd programmatically from a program, but I didn't get an explanation I could understand why this is not possible. Can someone explain why you can't invoke cd from a program?

cd is part of your shell, so trying to invoke cd from another program would be like opening a new browser tab from another program. It's an internal feature without an outward-facing API.

Re: How does `cd` work?

#10
post #3

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

[deleted]
Post reply on HN