bash 如何在shell脚本中运行'cd'并在脚本完成后留在那里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3879431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run 'cd' in shell script and stay there after script finishes?
提问by qrtt1
I used 'change directory' in my shell script (bash)
我在 shell 脚本 (bash) 中使用了“更改目录”
#!/bin/bash
alias mycd='cd some_place'
mycd
pwd
pwd
prints some_place
correctly, but after the script finished my current working directory doesn't change.
pwd
打印some_place
正确,但脚本完成后,我当前的工作目录不会改变。
Is it possible to change my path by script?
是否可以通过脚本更改我的路径?
回答by codaddict
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.
如果不采购,更改将发生在子 shell 中,而不是在调用脚本的父 shell 中。但是,当您获取文件时,文件中的行将被执行,就好像它们是在命令行中键入的一样。
回答by schot
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:
该脚本在单独的子 shell 中运行。该子shell更改目录,而不是您运行它的shell。一个可能的解决方案是source
脚本而不是运行它:
# Bash
source yourscript.sh
# or POSIX sh
. yourscript.sh
回答by thomasd
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.
虽然获取您想要运行的脚本是一种解决方案,但您应该意识到该脚本可以直接修改当前 shell 的环境。也不能再传递参数了。
Another way to do, is to implement your script as a function in bash.
另一种方法是将您的脚本实现为 bash 中的函数。
function cdbm() {
cd whereever_you_want_to_go
echo arguments to the functions were , , ...
}
This technique is used by autojump: http://github.com/joelthelion/autojump/wikito provide you with learning shell directory bookmarks.
autojump 使用此技术:http://github.com/joelthelion/autojump/wiki 为您提供学习shell 目录书签。
回答by Fizer Khan
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.
它可以通过采购来实现。采购基本上是在同一个 shell 中执行脚本,而正常执行(sh test.sh
或./test.sh
)将创建子 shell 并在那里执行脚本。
test.sh
测试文件
cd development/
ls
# Do whatever you want.
Execute test.sh
by
执行test.sh
者
source test.sh
.
is shortest notation for source
. So you can also do by
.
是 的最短符号source
。所以你也可以通过
. test.sh
This will execute the script and change the directory of current shell to development/
.
这将执行脚本并将当前 shell 的目录更改为development/
.
回答by dig_123
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.
每当您在登录 shell 上运行脚本时,就会产生一个新的子进程,并在子 shell 中执行脚本。脚本完成后,子 shell 退出,您将返回到登录 shell。因此,每当您通过脚本,目录更改为 cd 指定的路径,但是当脚本完成时,您将返回登录 shell 到您启动脚本的工作目录。
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.
source 的作用是将脚本作为 TCL 脚本执行,即它与您在登录 shell 的命令行上键入每一行并从那里执行时具有相同的效果。因此,当脚本在 cd 之后完成时,它会保留在该目录中。