Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 702
user285728 Avatar asked Mar 03 '10 21:03

user285728


People also ask

How do I change parent directory?

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

Which command is used to show parent working directory in Linux?

The pwd command stands for print working directory. It is one of the most basic and frequently used commands in Linux.

Which command is used in parent directory?

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.


2 Answers

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.

like image 139
ephemient Avatar answered Oct 08 '22 14:10

ephemient


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.

like image 30
Vlad Avatar answered Oct 08 '22 15:10

Vlad