Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement bash command cd in perl

Tags:

bash

perl

I tried to implement a bash command system("cd /home/user") in perl , but I get an error saying

Can't exec "cd": No such file or directory at temp.pl 

Is there a way to change the current working directory to the specified one , and the change remains after the perl script has exited also.

like image 347
user3304726 Avatar asked Mar 27 '26 05:03

user3304726


1 Answers

No. A process can't change its parent process's current working directory. Shells implement commands like cd as "builtins", meaning they're a function in the shell itself, and not a separate process that gets run.

You can change the current directory in perl using chdir($dir), and that change will be inherited by child processes — but it won't be passed along to the parent process.

like image 74
hobbs Avatar answered Mar 29 '26 19:03

hobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!