In Script to change current directory (cd, pwd) it is shown how to run a bash script that changes terminal directory.
But how do i run a perl script that runs a bash script that changes terminal directory?
The Perl script runs in a process which is a child of your shell session. This child process can change its own working directory all it likes, but it cannot change it's parent's working directory. When the Perl script exits, control is returned to the parent process (the shell session), which will have remained in the same working directory the whole time, regardless of what the child process did while it was running.
You can't do this directly. The only way for a shell to change its current directory is for the shell itself to execute a
But you can do it indirectly. Here's a simple example that change the current directory to
This assumes that
When you type
A Perl script can't cause a calling shell to change directories, but it can provide it with a command that the shell can then execute itself.
Of course you can use a directory other than
One point of clarification: the current directory is a property of the current shell process, not of the terminal.
I just realized that I missed part of your question; you want "a perl script that runs a bash script". It's easy enough to modify my example to do that, but I have no idea why you need the Perl script to run a bash script. You haven't told us nearly enough about what you're actually trying to accomplish.
There are multiple ways (TIMTOWTDI) of executing external commands from Perl. Like using
Some questions on "How to do it in other ways". For unusual requirements there ought to be unusual solutions (that work). But the question is why would you use Perl to change parent shell directory? Here's how you'd do it anyways
You can only do that indirectly, e.g. using aliases. Have your bash script, instead of changing to the desired directory, just print the desired directory to stdout. Perl should then capture this output and also print the desired directory to stdout. A shell alias or shell function can then take that output and change the directory inside the running shell.
Use this shell function and test it:
One more solution is to first save the directory you want to visit in a string variable :