8 ms·
Something to note which wasn't mentioned here is that `cd` is not a binary executable, but instead a shell command. You won't find it anywhere in /bin, /sbin an
by StuckDuck 4y ago
Something to note which wasn't mentioned here is that `cd` is not a binary executable, but instead a shell command. You won't find it anywhere in /bin, /sbin and so on. That's also why `sudo cd ` doesn't work, `sudo` will try to execute `cd` as root but it's not an executable so it won't find it. (tip: do `sudo -i` instead)
- rjsw 4y agoAn executable of 'cd' wouldn't do anything useful, it could only change its own idea of working directory then exit, the shell or other executable that invoked it would be unchanged.
- stjohnswarts 4y agowhy couldn't it call an api in the shell to change directory?
- remram 4y agoWhat would that improve? And what happens if you call it from another shell?
- stjohnswarts 4y agoI never implied that it was better or worse but that it was possible
- remram 4y agocd is a good example of something that has to be a built-in command, because it makes the shell itself do something (change its current working directory). Other commands in that category are umask, the job control ones (fg, bg, wait, ...), the ones setting variables (read, printf), and the control flow ones (if, while, break, return, exit, ...). A lot of commands are also built-in for convenience but don't strictly need to be.
- daptaq 4y ago> cd is a good example of something that has to be a built-in command Theoretically you could also have a cd that would IPC the shell to change the pwd but I guess you could also say that is overengineering.
- remram 4y agoThat sounds like a weird way to implement a built-in command (the IPC handler still needs to be built-in, so you didn't really remove it from the shell). And then if course that binary would be available globally but only work if you're using the shell it's built for...
- daptaq 4y agoYeah, that is why it isn't done. I am just pointing out the pointless fact that you could write a cd command that wasn't itself built-in.
- remram 4y agoYou could hook up to the parent process with ptrace or the equivalent debugging API of your OS of choice and forcibly change the directory. Stackoverflow even has an implementation: https://unix.stackexchange.com/a/114262/81005 https://unix.stackexchange.com/a/114262/81005
- yewenjie 4y agoI'm curious if this is true for every alternative new niche shells like murex, oil, nushell etc?
- emmelaich 4y agoAnd yet ... https://unix.stackexchange.com/questions/50058/what-is-the-point-of-the-cd-external-command https://unix.stackexchange.com/questions/50058/what-is-the-p... Yes, it's idiotic.
- 4y ago
- saagarjha 4y agoMany platforms will ship a (mostly) useless cd binary.