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

发布于 2024-09-26 14:33:50 字数 213 浏览 8 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜唯美灬不弃 2024-10-03 14:33:50

您需要将文件作为源:

. myfile.sh

或者

source myfile.sh

如果不源,更改将发生在子 shell 中,而不是调用脚本的父 shell 中。但是,当您获取文件时,文件中的行将像在命令行中键入一样执行。

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.

幻想少年梦 2024-10-03 14:33:50

虽然获取要运行的脚本是一种解决方案,但您应该意识到该脚本可以直接修改当前 shell 的环境。而且也不可能再传递参数了。

另一种方法是将脚本实现为 bash 中的函数。

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

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.

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.

Smile简单爱 2024-10-03 14:33:50

该脚本在单独的子 shell 中运行。该子 shell 会更改目录,而不是运行它的 shell。一个可能的解决方案是source 脚本而不是运行它:

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

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
樱花细雨 2024-10-03 14:33:50

可以通过采购来实现。采购基本上是在同一个 shell 中执行脚本,而正常执行(sh test.sh./test.sh)将创建子 shell 并在那里执行脚本。

test.sh

cd development/
ls
# Do whatever you want.

通过执行test.sh

source test.sh

。是source的最短表示法。所以你也可以这样做,

. test.sh

这将执行脚本并将当前 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

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/.

难以启齿的温柔 2024-10-03 14:33:50

每当您在登录 shell 上运行脚本时,都会生成一个新的子进程,并且脚本执行在子 shell 中完成。脚本完成后,子 shell 退出,您将返回到登录 shell。因此,每当您通过脚本时,目录将更改为 cd 指定的路径,但是当脚本完成时,您将返回到登录 shell 到启动脚本的工作目录。

克服这个问题的方法是使用,

source yourscript.sh

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,

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.

将军与妓 2024-10-03 14:33:50

另一个实用的解决方案是通过打开另一个 shell 会话来结束脚本。
例如:

#!/bin/bash
cd some_place
bash

就我而言,这对于位于我的 ~/bin 中的脚本(例如从任何其他地方调用)很有用。只是输入 source ~/bin/mygoodoldscript 而不是 mygoo 有点痛苦。

缺点是额外的 shell 会占用更多资源(不多)。

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).

一城柳絮吹成雪 2024-10-03 14:33:50

虽然有答案。我认为问题的目的是使用脚本导航到特定路径。

这是一个简单实用的解决方案,无需取消现有的终端环境标志。

  1. 提供 bash/tch/sh 脚本来生成路径
/* .goto.sh */
#!/usr/bin/env bash
echo '~/workspace'
  1. 将别名添加到脚本输出
alias goto 'cd `.goto.sh`'

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`'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文