如何在 shell 脚本中运行“ cd”并在脚本完成后停留在那里?

我在我的 shell 脚本(bash)中使用了“更改目录”

#!/bin/bash
alias mycd='cd some_place'
mycd
pwd

pwd正确地打印出 some_place,但是在脚本完成之后,我当前的工作目录不会改变。

有没有可能通过脚本改变我的路径?

74626 次浏览

The script is run in a separate subshell. That subshell changes directory, not the shell you run it in. A possible solution is to source the script instead of running it:

# Bash
source yourscript.sh
# or POSIX sh
. yourscript.sh

You need to source the file as:

. myfile.sh

or

source myfile.sh

Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.

While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

Another way to do, is to implement your script as a function in bash.

function cdbm() {
cd whereever_you_want_to_go
echo arguments to the functions were $1, $2, ...
}

This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

whenever you run a script on your login shell, a new subprocess is spawned and the script execution is done in a subshell.Once the script completes, the subshell exits and you are returned to the login shell.Hence whenever you do a cd through a script,the directory is changed to the path specified by cd, but by the time script finishes you come back to your login shell to the working directory from where you started the script.

The way to overcome this is use,

source yourscript.sh

what source does is it executes the script as TCL script, i.e it has the same effect as when you typed each line on the command line of your login shell and it executed from there. So this way when the script finishes after cd , it stays in that directory.

It can be achieved by sourcing. Sourcing is basically execute the script in the same shell whereas normal execution(sh test.sh or ./test.sh) will create sub shell and execute script there.

test.sh

cd development/
ls
# Do whatever you want.

Execute test.sh by

source test.sh

. is shortest notation for source. So you can also do by

. test.sh

This will execute the script and change the directory of current shell to development/.

Another practical solution is to end your script by opening another shell session. For instance:

#!/bin/bash
cd some_place
bash

This is useful, in my case, for scripts located in my ~/bin for instance, called from any other place. It is just a bit painful to type source ~/bin/mygoodoldscript instead of mygoo<TAB><ENTER>.

The downside is that the additional shell takes up a few more resources (not much).

Though there are answers. I think the intention of question is to use script to navigate to specific path.

Here is a simple practical solution works here without cancel out existing terminal environment flag.

  1. provide a bash/tch/sh script to work for path generation
/* .goto.sh */
#!/usr/bin/env bash
echo '~/workspace'
  1. add alias to the script output
alias goto 'cd `.goto.sh`'