更改父外壳的目录 [英] Changing a directory of the parent shell
问题描述
我想知道一种可用于从子外壳更改父外壳目录的机制。例如,我正在$ HOME中运行脚本 settings.sh 。我的$ HOME有一个目录$ HOME / TEST / run。如果我的 settings.sh 脚本如下
I 'm wondering of any mechanism that one could use to change the directory of a parent shell from sub-shell. For ex., I 'm running the script "settings.sh" in my $HOME. My $HOME has a directory $HOME/TEST/run. If my "settings.sh" scripts is as below
#!/bin/bash
# some shell related code
# some shell related code
cd $HOME/TEST/run
exit 0
我在$ HOME的命令提示符处执行以上脚本。执行后,我希望我的命令提示符在目录$ HOME / TEST / run中。我确实知道在sub-shell中,它被CD刻录到$ HOME / TEST / run,但是在执行结束时,它又回到$ HOME。
I execute the above script at command prompt at $HOME. After the execution, I expect my command prompt in directory $HOME/TEST/run. I do understand that in sub-shell, it is being cd'd to $HOME/TEST/run, but at the end of the execution, it's back in $HOME.
使用单个脚本是否可以通过任何优雅的方式完成上述操作。 一种方法是修改脚本 settings.sh以生成另一个脚本,然后使用。$ HOME / generatedScript.sh
Is there any elegant way of doing the above, using a single script. One way is to modify the script "settings.sh" to generate another script and then use ". $HOME/generatedScript.sh"
推荐答案
不,您不能。那是设计使然。父进程永远不会受到子进程的影响而不会受到影响(否则子shell会对父进程造成各种令人讨厌的棘手事情)。
Nope, you can't. That's by design. Parent processes should never be affected by the results of a child without them wanting to be affected (otherwise sub-shells could do all sorts of nasty tricky things to the parent).
您可以做的是让外壳程序将信息保存到文件中或打印目录或...,以便如果父母希望,父母至少可以使用它来更改目录。
What you can do is have the shell save the information into a file or print the directory or ... Such that the parent at least can use it to change directories if the parent wants to.
这篇关于更改父外壳的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!