Perl script, do cd on terminal
Solution 1
You can't.
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.
Solution 2
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 cd
command (or pushd
, popd
, etc.).
But you can do it indirectly. Here's a simple example that change the current directory to /tmp
:
cd-tmp.pl:
#!/usr/bin/perl
print "cd /tmp\n";
In your .bashrc
or .bash_profile
:
cd-tmp() { eval $(cd-tmp.pl); }
This assumes that cd-tmp.pl
is in a directory in your $PATH
-- or you can specify the full path in the function.
When you type cd-tmp
at your shell prompt, it invokes cd-tmp.pl
, captures its output, and executes that output as a shell command.
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 /tmp
, including one that's determined based on other information or created on the fly.
One point of clarification: the current directory is a property of the current shell process, not of the terminal.
UPDATE :
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.
Related videos on Youtube
coder
Updated on September 18, 2022Comments
-
coder over 1 year
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?
-
manatwork almost 12 yearsHave you also read changing current working dir with a script?
-
coder almost 12 yearsyeah and its not really answering the question. I need to run a perl script which changes directory on the terminal.
-
manatwork almost 12 yearsNo, is not answering your question. But points out an important detail: when the process which changed the work directory terminates, the parent will continue its job with its own unchanged environment. So if you
chdir
in aperl
script, that will have no effect in thebash
which started it. Are you sure this is fine for you? -
coder almost 12 yearsI am asking how I can have an effect in the bash that started it.
-
Matteo almost 12 years@HermannIngjaldsson Yes, both an answer and the comments are answering your question: "you can't". Perl, shell, whatever: you cannot change the working directory of your process parent shell.
-
Mat almost 12 years@HermannIngjaldsson: do you mean perldoc.perl.org/functions/chdir.html ?
-
jw013 almost 12 years
-
-
coder almost 12 yearsAnd what are those ways to affect the parent shell?
-
Keith Thompson almost 12 yearsYour
b.pl
script would be more simply written assystem './a.sh'
-- and it's not particularly useful, since you might as well just invokea.sh
directly. -
bnikhil almost 12 yearsWell, it was just for demonstration. The OP wanted to have a Perl script that runs a shell script that changes the current directory of the shell running the perl script.
-
Keith Thompson almost 12 yearsAh, I missed the "that runs a shell script" part. Which leaves an open question: Why??