如何运行“cd”在 shell 脚本中并在脚本完成后保留在那里?
我在 shell 脚本 (bash) 中使用了“更改目录”,
#!/bin/bash
alias mycd='cd some_place'
mycd
pwd
pwd
正确打印了 some_place
,但在脚本完成后,我当前的工作目录没有更改。
是否可以通过脚本更改我的路径?
I used 'change directory' in my shell script (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.
Is it possible to change my path by script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要将文件作为源:
或者
如果不源,更改将发生在子 shell 中,而不是调用脚本的父 shell 中。但是,当您获取文件时,文件中的行将像在命令行中键入一样执行。
You need to source the file as:
or
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 的环境。而且也不可能再传递参数了。
另一种方法是将脚本实现为 bash 中的函数。
autojump 使用了该技术:
http://github.com/joelthelion/autojump/wiki
为您提供学习shell目录书签。
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.
This technique is used by autojump:
http://github.com/joelthelion/autojump/wiki
to provide you with learning shell directory bookmarks.
该脚本在单独的子 shell 中运行。该子 shell 会更改目录,而不是运行它的 shell。一个可能的解决方案是
source
脚本而不是运行它: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 中执行脚本,而正常执行(
sh test.sh
或./test.sh
)将创建子 shell 并在那里执行脚本。test.sh
通过
执行
test.sh
。是
source
的最短表示法。所以你也可以这样做,这将执行脚本并将当前 shell 的目录更改为
development/
。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
Execute
test.sh
by.
is shortest notation forsource
. So you can also do byThis will execute the script and change the directory of current shell to
development/
.每当您在登录 shell 上运行脚本时,都会生成一个新的子进程,并且脚本执行在子 shell 中完成。脚本完成后,子 shell 退出,您将返回到登录 shell。因此,每当您通过脚本时,目录将更改为 cd 指定的路径,但是当脚本完成时,您将返回到登录 shell 到启动脚本的工作目录。
克服这个问题的方法是使用,
source 的作用是将脚本作为 TCL 脚本执行,即它与您在登录 shell 的命令行上键入每一行并从那里执行时具有相同的效果。这样,当脚本在 cd 之后完成时,它会保留在该目录中。
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,
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.
另一个实用的解决方案是通过打开另一个 shell 会话来结束脚本。
例如:
就我而言,这对于位于我的
~/bin
中的脚本(例如从任何其他地方调用)很有用。只是输入source ~/bin/mygoodoldscript
而不是mygoo
有点痛苦。缺点是额外的 shell 会占用更多资源(不多)。
Another practical solution is to end your script by opening another shell session.
For instance:
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 typesource ~/bin/mygoodoldscript
instead ofmygoo<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.