News for hackers. Stuff that matters.
Re:Linux command line for you and me by StuckDuck on Sunday September 18, 2022 @09:13AM (#32887263)
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)
Re:Linux command line for you and me by remram on Sunday September 18, 2022 @09:59AM (#32887609)
cd 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.

Re:Linux command line for you and me by daptaq on Sunday September 18, 2022 @10:34AM (#32887892)
> 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.

Re:Linux command line for you and me by remram on Sunday September 18, 2022 @01:35PM (#32889528)
That 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...
Re:Linux command line for you and me by daptaq on Sunday September 18, 2022 @05:55PM (#32891860)
Yeah, 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.
Re:Linux command line for you and me by remram on Sunday September 18, 2022 @09:28PM (#32893257)
You 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
Re:Linux command line for you and me by emmelaich on Sunday September 18, 2022 @10:08AM (#32887691)
Re:Linux command line for you and me by version_five on Sunday September 18, 2022 @10:37AM (#32887926)
The question and highest rated reply there are very interesting and worth reading (at least for trivia value), thanks for that.

I don't understand what you meant is idiotic though

Re:Linux command line for you and me by ianai on Sunday September 18, 2022 @10:51AM (#32888016)
As that comment said, it’s there so utilities can use it for checking the return of “chdir()”. It’s at best a name conflict with a shell command with a well established past and use. At worst, it probably enables some bad code smells or outright malicious code.
Re:Linux command line for you and me by emmelaich on Tuesday September 20, 2022 @02:29AM (#32908397)
I've seen questions about sudo (for instance) that ask why does "sudo chdir" not work.

By idiotic I think it's a mistake to blur the line between shell builtins and external commands.

Re:Linux command line for you and me by nixcraft on Sunday September 18, 2022 @10:14AM (#32887728)
We can use the `compgen -b` (`help cd` to get short help or `man bash-builtins` to read man pages) to list all internal bash commands. For example, we can use the `type -a cmd1` or `command -V cmd1` to see if `cmd1` is built-in or external binary.
Re:Linux command line for you and me by 0x445442 on Sunday September 18, 2022 @10:37AM (#32887925)
Don’t forget “dirs -v”. I’ve tried to use Powershell a few times and always tap out because it doesn’t come with the dirs command. It’s so fundamental to my workflow a shell without it or without an easy way to implement it is a no go.
Re:Linux command line for you and me by yewenjie on Friday September 23, 2022 @08:27AM (#32950976)
I'm curious if this is true for every alternative new niche shells like murex, oil, nushell etc?
Re:Linux command line for you and me by rjsw on Sunday September 18, 2022 @09:19AM (#32887321)
An 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.
Re:Linux command line for you and me by stjohnswarts on Sunday September 18, 2022 @12:13PM (#32888721)
why couldn't it call an api in the shell to change directory?
Re:Linux command line for you and me by remram on Sunday September 18, 2022 @01:37PM (#32889543)
What would that improve? And what happens if you call it from another shell?
Re:Linux command line for you and me by stjohnswarts on Monday September 19, 2022 @12:10PM (#32900257)
I never implied that it was better or worse but that it was possible
Re:Linux command line for you and me by saagarjha on Sunday September 18, 2022 @10:14AM (#32887729)
Many platforms will ship a (mostly) useless cd binary.