Live data from Hacker News

How does `cd` work?

blog.safia.rocks

11–20 of 93 posts

Re: How does `cd` work?

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

There's no reason why one shouldn't be able to invoke the chdir system call and implement the same behaviour.

Re: How does `cd` work?

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

The explanation is at the end of the linked page.

As far as I know, you will need to provide the user with an alias or a function or something that can put in their .bashrc or something, and there is no other way to do it. (Anything that could would constitute a violation of the UNIX process model.)

Re: How does `cd` work?

#17

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/

Re: How does `cd` work?

#18
post #4

Here's cd implemented as a stand-alone program: https://github.com/robertswiecki/extcd

This looks fiendishly clever, if it really works, but I don't have time to dig through the 'ptrace' man page to figure out what it's doing. Can someone summarize?

Re: How does `cd` work?

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

For the same reason you can't invoke a method to change the value of a string, or open a file handle, in another process:

You can't change another process's internal state without sending a message that the other process can choose to interpret (or by hacking the memory that the process is using)

Re: How does `cd` work?

#20
post #12

Earlier quoted context omitted.

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.

There's no reason why one shouldn't be able to invoke the chdir system call and implement the same behaviour.

Allowing any process to arbitrarily modify the state of another process would be a disaster for stability and security.
Post reply on HN