Perl changes the working directory of the caller

I want to write a perl script that changes its working directory to another place, does something, and then leaves me in this directory after a call from the shell. chdironly the first part. How to change the working directory of the caller?

+3
source share
4 answers

What you want to do is impossible. The closest thing would be to write bash, which will do what you want, and then in the calling shell, run it instead. Software cannot affect the shell that invokes it.

+5
source

It is possible.

/dev/mem /, , [dev,ino] cwd, .

, , . , .:) , - . . . , , .

.:)

+9

perl script script, bash script perl script script.

, Perl script :

#!/usr/bin/perl
# my_program.pl ...
chdir "/my/directory";
...

, , bash script :

#!/usr/bin/bash
cd /my/directory
perl my_program.pl "$@"

script, , ..

$ source my_bash.sh

$ . my_bash.sh

(Now you could use the heredoc syntax and put it in one script:

#!/usr/bin/bash
cd /my/directory
perl <<EOF
... include your perl script here ...
EOF

But I don’t think you could use variables @ARGV)

+5
source

You can do followng to simulate the effect:

  • Call script using exec
  • In the script, instead of exiting, release a new shell.
+2
source

All Articles