Live data from Hacker News

How does `cd` work?

blog.safia.rocks

41–50 of 93 posts

Re: How does `cd` work?

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

Re: How does `cd` work?

#42
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…

[deleted]

Re: How does `cd` work?

#43

Random piece of info about cd: cd.. (no space between "cd" and "..") works on Windows

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?

#44
post #35

> I decided to dive into the code for the Bourne shell to see what I might be able to figure out about these builtins. I came across the definition of the cd builtin here. (link to: http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/cd.d... ) no, you came across the definition of bash's 'cd' builtin there.. On FreeBSD with a source checkout, the definition of /bin/sh's (the 'ash' shell, https://en.wikipedia.org/…

I'm pretty sure OP uses OS X (or whatever it's called these days), which has bash as the default shell.

Re: How does `cd` work?

#45
post #40
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…

https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/a... One of the most famous shell-portability issues is related to "$@". When there are no positional arguments, Posix says that "$@" is supposed to be equivalent to nothing, but the original Unix version 7 Bourne shell treated it as equivalent to "" instead, and this behavior survives in later implementations like Digital Unix 5.0. The traditional way to wor…

Thanks for digging that up! But for scripts written for FreeBSD in 2002 [1], I have to wonder which non-POSIX-y shells they were worried about.

[1]: https://github.com/freebsd/freebsd/commit/55d0b8395514ae4055...

Re: How does `cd` work?

#46
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.

Re: How does `cd` work?

#47
post #35

> I decided to dive into the code for the Bourne shell to see what I might be able to figure out about these builtins. I came across the definition of the cd builtin here. (link to: http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/cd.d... ) no, you came across the definition of bash's 'cd' builtin there.. On FreeBSD with a source checkout, the definition of /bin/sh's (the 'ash' shell, https://en.wikipedia.org/…

Yet another example where the BSD code is simpler and clearer.

I don't think it's particularly controversial to say that when you want to explore how the internals of unix work, you're almost always better off reading the BSD source code first. Even if you don't run a BSD. (musl libc is great, too, but limited to libc.)

I do most of my systems programming for Linux platforms, but when I have a question about semantics my first stop is POSIX to learn how it should behave. My second stop is the BSD source so I can quickly grok the mechanics. Lastly would be the Linux or GNU code, to confirm exact behavior. Setting aside the fact that Linux or GNU code usually feels like it was written inside-out and upside-down, you can't well understand why and how something works without having a more general understanding of the problem and solution space. This applies to everything in life, but in the context of systems software programming I've developed a very concrete process.

Having a copy of POSIX locally (greppable, but also the local HTML frames version is super easy to navigate), as well as easy access to BSD code in /usr/src, can make this a very fast and efficient process. Much faster than Googling, wading through Stack Overflow, and other haphazard habits.

Re: How does `cd` work?

#48
post #31
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…

> If /usr/bin is on a case-insensitive filesystem, and you execvp("CD", ...) That seems like fairly rare edge case, considering that UNIX typically had case-sensitive file-systems, or rather handled filenames as opaque blobs. I wonder what actual system caused the need for case folding? Maybe HFS?

The `tr` bit isn't actually in the FreeBSD version of the script[1], so I'm assuming it's a macOS HFS thing.

https://github.com/freebsd/freebsd/blob/master/usr.bin/alias...

Re: How does `cd` work?

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

I have had this snippet in my `.bashrc` for years. No idea who to give credit to: # eg. save mc # cd mc # no '$' is necessary if [ ! -f ~/.dirs ]; then # if doesn't exist, create it touch ~/.dirs fi alias show='cat ~/.dirs' save (){ command sed "/!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "$@"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ; source ~/.dirs # Initialization for the above 'save' facility: source the .s…

https://github.com/wting/autojump does something similar, just automatically (and better, IMO).

Re: How does `cd` work?

#50
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.

HN is not immune to cargo cults. SV and modern "tech culture" applauds people for writing blog posts like this: "Today I decided to find out what makes the sky blue! I just thought I'd write a blog post about it." Then the culture is reinforced, as when you mention how it's stupid to upvote the blog of someone who literally admits they do not know what they are talking about, you get shouted down for not handing out participation trophies. And yes, I realize how rude this comment is, and how stereotypically anti-millennial it is. But it's what has been happening on HN for years.
Post reply on HN