Hacker News new | ask | show | jobs
by jsnk 3028 days ago
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?

5 comments

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 .sdirs file
    }
    source ~/.dirs  # Initialization for the above 'save' facility: source the .sdirs file
    shopt -s cdable_vars # set the bash option so that no '$' is required when using the above facility
What this does is, whenever you're in a directory that you'd like to "bookmark" as you'd call it. Just type `save whatevername`. Then, when you navigate somewhere else you can type `cd whatevername` and it'll change you back there. It's simply adding to this ~/.dirs file and so overwriting is taken care of by just saving the same name in a new (or the same) directory. It just appends a line.

Also, you can just type `show` and any point and it'll tell you what you've saved and where.

The nicest thing is that this persists with new logins (the only drawback is that other shells that are running don't get the update automatically).

https://github.com/wting/autojump does something similar, just automatically (and better, IMO).
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)

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

Letting a program arbitrarily modify another program's working directory or its environment variables would be a nightmare for any program that accesses files from relative paths, I think it's pretty obvious why you wouldn't be allowed to do something like that.
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.
Well, I mean, a program can call chdir() and it works exactly as it's supposed to, but it only affects that program, not a different program.
Allowing any process to arbitrarily modify the state of another process would be a disaster for stability and security.
Isn’t that what a debugger does?
Yes, and it is a disaster for stability and security. The designers of dtrace worked hard on the stability part. See, for example, the section on DIF safety in https://www.cs.princeton.edu/courses/archive/fall04/cos518/p....