2

I am newbie in perl, how can I change the current working directory in terminal also using perl. I want to provide input from terminal, so it will take respective dir.

use Cwd;
my $in1 = shift;
my $in2 ="/usr/swarak/scripts/dir";
my $str = $in2.$in1;
print "$str\n";
chdir "$str";
print(cwd);

this is changing the present script executing dir, I want to change the Linux terminal also.

OUTPUT: if I give 1, it should take me /usr/swarak/scripts/dir1, if I give 99, /usr/swarak/scripts/dir99.

I have lost of dir, so want this simple shortcut.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
swarak
  • 89
  • 5
  • 3
    You can't do that this way, as the Perl script interpreter will run in a different process (than the bash), and when it leaves its environment is destroyed (ie the caller environment, CWD... is unchanged). Use bash functions... [provided I didn't misunderstood your question!] – Déjà vu Aug 27 '20 at 07:13
  • See also [perl how to finish the script by 'cd $newdir'](https://stackoverflow.com/q/33372743/2173773) and [implement bash command cd in perl](https://stackoverflow.com/q/24133768/2173773) – Håkon Hægland Aug 27 '20 at 07:35
  • Thank you very much for commenting, I understand now, how do we do it in bash script ? – swarak Aug 27 '20 at 07:44
  • You could add this to the end of your `perl` script: `print "Starting subshell, 'exit' when you are done.\n"; exec $ENV{SHELL};` to simulate what you want to do. – Ted Lyngmo Aug 27 '20 at 08:32

1 Answers1

2

Each process on a Linux server runs in an environment. The current working directory is one of the attributes of that environment.

When a new process starts (and your Perl program runs as a new process), it inherits the current environment from its parent process. The new process can then make whatever changes it wants to its new environment. It cannot, however, change its parent's environment. When a process ends, its environment is destroyed.

So, what happens here:

  • You have a process (your shell window, for example) which has its environment. The current working directory is... well, let's say your home directory.
  • You run your Perl program. That inherits a copy of the existing environment, with the current working directory set to your home directory.
  • You program changes directory to your new directory. This is in the subprocess environment. It does not affect the parent environment.
  • Your program ends and exits. The subprocess environment is destroyed.
  • You are back in your shell window environment. This environment hasn't changed. Therefore, your current working directory hasn't changed.

This is something that all Unix/Linux users have to find out for themselves at some point. You cannot write a program that changes the current working directory of its parent process.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thank you very much for explaining the environments, I understand the limits now. But for this problem any other way to do it ? I tried aliasing in cshrc but failed, and csh dont support functions to do in functions, I dont have bash, and cannot install it. – swarak Aug 27 '20 at 07:42
  • @swarak: You could use the approach described here - https://stackoverflow.com/questions/28135356/change-working-directory-from-csh-script. Have your program print the directory and use that together with `source`. – Dave Cross Aug 27 '20 at 07:52
  • it get till here, ````#/usr/bin/csh` set num = "$argv" set scr = (/usr/swarak/scripts/dir) set scrp = "$scr$num" echo "$scrp" set jobdir = `dirname $scrp` cd $jobdir from terminal: source cdn.csh 1 this is going till /usr/swarak/scripts, and printing correct dir, /usr/swarak/scripts/dir1 in echo, but it is not going inside dir1 – swarak Aug 31 '20 at 05:28
  • @swarak: Please don't put code in comments. It's really hard to read. Instead, [edit your question](https://stackoverflow.com/posts/63611017/edit) to add the new code. – Dave Cross Aug 31 '20 at 09:06
  • @swarak: You have just written a csh version of your Perl code. It doesn't work for **exactly** the same reason. A subprocess cannot change the environment of its parent process. – Dave Cross Aug 31 '20 at 09:08