Perl script, do cd on terminal

8,785

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.

Share:
8,785

Related videos on Youtube

coder
Author by

coder

Updated on September 18, 2022

Comments

  • coder
    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
      manatwork almost 12 years
    • coder
      coder almost 12 years
      yeah and its not really answering the question. I need to run a perl script which changes directory on the terminal.
    • manatwork
      manatwork almost 12 years
      No, 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 a perl script, that will have no effect in the bash which started it. Are you sure this is fine for you?
    • coder
      coder almost 12 years
      I am asking how I can have an effect in the bash that started it.
    • Matteo
      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
      Mat almost 12 years
      @HermannIngjaldsson: do you mean perldoc.perl.org/functions/chdir.html ?
    • jw013
      jw013 almost 12 years
  • coder
    coder almost 12 years
    And what are those ways to affect the parent shell?
  • Keith Thompson
    Keith Thompson almost 12 years
    Your b.pl script would be more simply written as system './a.sh' -- and it's not particularly useful, since you might as well just invoke a.sh directly.
  • bnikhil
    bnikhil almost 12 years
    Well, 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
    Keith Thompson almost 12 years
    Ah, I missed the "that runs a shell script" part. Which leaves an open question: Why??