在perl中实现bash命令cd [英] implement bash command cd in perl
问题描述
我试图在perl中实现bash命令 system("cd/home/user")
,但是出现错误提示
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
是否可以将当前工作目录更改为指定的目录,并且在退出perl脚本后仍保留更改.
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.
推荐答案
否.进程无法更改其父进程的当前工作目录.外壳程序将诸如 cd
之类的命令实现为"builtins",这意味着它们是外壳程序本身的功能,而不是单独运行的进程.
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.
您可以使用 chdir($ dir)
在perl中更改当前目录,并且该更改将由子进程继承-但不会传递给父进程.
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.
这篇关于在perl中实现bash命令cd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!