1

这是场景,我打开一个终端,bash'scwd是 `/home/me',像这样:

[/home/me $] ls
a.sh

现在我a.sh在 cwd 中是这样的:

[/home/me $] ./a.sh

我想要的是,我想通过运行来改变bash's ,这意味着完成后,'s不会是,cwda.sha.shbashcwd/home/me

[/home/other_dir $]

我可以这样做吗?如果是,如何?

4

2 回答 2

1

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

于 2012-10-23T10:01:18.880 回答
1

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.

于 2012-10-23T10:01:21.310 回答