How do I set the working directory of the parent process?

12,092

Solution 1

Don't do this.

FILE *p;
char cmd[32];
p = fopen("/tmp/gdb_cmds", "w");
fprintf(p, "call chdir(\"..\")\ndetach\nquit\n");
fclose(p);
sprintf(cmd, "gdb -p %d -batch -x /tmp/gdb_cmds", getppid());
system(cmd);

It will probably work, though note that Bash's pwd command is cached and won't notice.

Solution 2

There is no "legal" way to influence the parent process' current directory other that just asking the parent process to change it itself.

chdir which changes the directory in bash scripts is not an external utility, it's a builtin command.

Solution 3

The chdir command is a shell built-in, so it has direct access to the working directory of the shell that executes it. Shells are usually pretty good at protecting themselves from the effects of scripts, giving the child process a copy of the shell's own working environment. When the child process exits, the environment it used is deleted.

One thing you can do is 'source' a script. This lets you change the directory because in essence, you are telling the shell to execute the commands from the file as though you had typed them in directly. I.e., you're not working from a copy of the shell's environment, you are working directly on it, when sourcing.

Solution 4

How exactly could you change the working directory of bash (or parent in general)?

It is not possible by using any "acceptable" way. By acceptable, I mean "without outrageously hacking your system (using gdb for example)" ;)

More seriously, when the user launch an executable, the child process will run in its own environment, which is mostly a copy of its parent environment. This environment contains "environment variables" as well as the "current working directory", just to name those two.

Of course, a process can alter its own environment. For example to change its working directory (like when you cd xxx in you shell). But since this environment is a copy, that does not alter the parent's environment in any way. And there is no standard way to modify your parent environment.


As a side note, this is why cd ("chdir") is an internal shell command, and not an external utility. If that was the case, it wouldn’t be able to change the shell's working directory.

Solution 5

The way I solved this is to have a shell alias that calls the script and source a file that the script wrote. So, for instance,

function waypoint {
    python "$WAYPOINT_DIRECTORY"/waypoint.py $@ &&
    source ~/.config/waypoint/scratch.sh
    cat /dev/null > ~/.config/waypoint/scratch.sh
}

and waypoint.py creates scratch.sh to look like

cd /some/directory

This is still a Bad Thing.

Share:
12,092
user285728
Author by

user285728

Updated on June 13, 2022

Comments

  • user285728
    user285728 about 2 years

    As the title reveals it, we are writing a Unix-style shell utility U that is supposed to be invoked (in most cases) from bash.

    How exactly could U change the working directory of bash (or parent in general)?

    P.S. The shell utility chdir succeeds in doing exactly the same, thus there must be a programmatic way of achieving the effect.