thirtySeven has asked for the wisdom of the Perl Monks concerning the following question:

I would like to change the directory of the parent process from within my perl script, so when the script is done executing the user is in a new directory. Is this possible? I would prefer to only use base modules.
  • Comment on Is it possible to change the directory of the parent process on Linux?

Replies are listed 'Best First'.
Re: Is it possible to change the directory of the parent process on Linux?
by hippo (Archbishop) on Mar 10, 2021 at 13:48 UTC
      ok I understand.
Re: Is it possible to change the directory of the parent process on Linux?
by bliako (Abbot) on Mar 10, 2021 at 14:34 UTC

    Unlikely but if your parent process is a unix shell and you want to end it as soon as the perl script ends, then end your perl script with exec '/bin/bash --ini-file somefile' and somefile contains cd /. You can do without the file by hacking something like this: bash$ /bin/bash --init-file <(echo "cd/"). In this way you will end up with a new shell and in dir /

      Yes, this orders the shell to *replace itself* ("exec ...") with another one ... which might do more changes than you want, such as losing environment-variable settings that are not made by .bashrc. Another common way to do it is to simply put the command which invokes the Perl script into a "two-liner" command-file which ends with a cd command. The Perl script is run, then the current-directory is changed, then the command-file ends, and you're still in the same shell instance.