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…
How does `cd` work?
41–50 of 93 posts
Re: How does `cd` work?
#42On 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…
Re: How does `cd` work?
#43Random 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/
Re: How does `cd` work?
#44> 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/…
Re: How does `cd` work?
#45Not 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…
[1]: https://github.com/freebsd/freebsd/commit/55d0b8395514ae4055...
Re: How does `cd` work?
#46On 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 am quite surprised/curious how it got to top page here.
Re: How does `cd` work?
#47> 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 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?
#48Not 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?
https://github.com/freebsd/freebsd/blob/master/usr.bin/alias...
Re: How does `cd` work?
#49I 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…
Re: How does `cd` work?
#50On 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.