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.
To change to a subdirectory, type cd, a space, and the name of the subdirectory (e.g., cd Documents) and then press [Enter]. To change to the current working directory's parent directory, type cd followed by a space and two periods and then press [Enter].
The pwd command stands for print working directory. It is one of the most basic and frequently used commands in Linux.
cd .. : this command is used to move to the parent directory of current directory, or the directory one level up from the current directory. “..” represents parent directory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With