这是场景,我打开一个终端,bash
'scwd
是 `/home/me',像这样:
[/home/me $] ls
a.sh
现在我a.sh
在 cwd 中是这样的:
[/home/me $] ./a.sh
我想要的是,我想通过运行来改变bash
's ,这意味着完成后,'s不会是,cwd
a.sh
a.sh
bash
cwd
/home/me
[/home/other_dir $]
我可以这样做吗?如果是,如何?
这是场景,我打开一个终端,bash
'scwd
是 `/home/me',像这样:
[/home/me $] ls
a.sh
现在我a.sh
在 cwd 中是这样的:
[/home/me $] ./a.sh
我想要的是,我想通过运行来改变bash
's ,这意味着完成后,'s不会是,cwd
a.sh
a.sh
bash
cwd
/home/me
[/home/other_dir $]
我可以这样做吗?如果是,如何?
You can use source command which runs in the current shell's context. So all the change done in the script will be visible in the your shell.
For example:
If the content of a.sh is: cd /usr/bin
then executing the following will result in change in CWD
$ source a.sh
For that you need to run a.sh
without forking a subshell like this:
. ./a.sh
This will make sure to run your script in current shell itself and and change dir
command inside your script will be reflected in current shell.