News for hackers. Stuff that matters.
Re:How does `cd` work? by jsnk on Thursday March 01, 2018 @02:27PM (#16495282)
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?

Re:How does `cd` work? by jsweojtj on Thursday March 01, 2018 @03:30PM (#16495945)
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).

Re:How does `cd` work? by orf on Thursday March 01, 2018 @03:50PM (#16496182)
https://github.com/wting/autojump does something similar, just automatically (and better, IMO).
Re:How does `cd` work? by gowld on Thursday March 01, 2018 @02:47PM (#16495480)
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? by jerf on Thursday March 01, 2018 @02:37PM (#16495388)
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? by steinuil on Thursday March 01, 2018 @03:23PM (#16495879)
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.
Re:How does `cd` work? by ChristianBundy on Thursday March 01, 2018 @02:31PM (#16495324)
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.
Re:How does `cd` work? by qubex on Thursday March 01, 2018 @02:36PM (#16495382)
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? by chc on Thursday March 01, 2018 @02:59PM (#16495624)
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.
Re:How does `cd` work? by gowld on Thursday March 01, 2018 @02:48PM (#16495493)
Allowing any process to arbitrarily modify the state of another process would be a disaster for stability and security.
Re:How does `cd` work? by colejohnson66 on Thursday March 01, 2018 @09:19PM (#16498422)
Isn’t that what a debugger does?
Re:How does `cd` work? by Someone on Friday March 02, 2018 @03:47AM (#16500023)
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....