从 Bash 脚本更改当前目录
是否可以从脚本更改当前目录?
我想在 Bash 中创建一个用于目录导航的实用程序。 我创建了一个如下所示的测试脚本:
#!/bin/bash
cd /home/artemb
当我从 Bash shell 执行该脚本时,当前目录不会更改。 是否可以通过脚本更改当前的 shell 目录?
Is it possible to change current directory from a script?
I want to create a utility for directory navigation in Bash. I have created a test script that looks like the following:
#!/bin/bash
cd /home/artemb
When I execute the script from the Bash shell the current directory doesn't change. Is it possible at all to change the current shell directory from a script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我喜欢对不同的项目做同样的事情,而不需要启动新的 shell。
在您的情况下:
将 the_script 保存为:
然后使用以下命令启动它:
然后您使用相同的 shell 进入该目录。
I like to do the same thing for different projects without firing up a new shell.
In your case:
Save the_script as:
Then fire it up with:
Then you get to the directory using the same shell.
声明你的路径:
Declare your path:
当您启动脚本时,将创建一个仅继承您的环境的新进程。 当它结束时,它就结束了。 您当前的环境保持原样。
相反,您可以像这样启动脚本:
.
将在当前环境中评估脚本,因此它可能会被更改When you start your script, a new process is created that only inherits your environment. When it ends, it ends. Your current environment stays as it is.
Instead, you can start your script like this:
The
.
will evaluate the script in the current environment, so it might be altered您需要将脚本转换为 shell 函数:
原因是每个进程都有自己的当前目录,当您从 shell 执行程序时,它会在新进程中运行。 标准的“cd”、“pushd”和“popd”内置于 shell 解释器中,因此它们会影响 shell 进程。
通过使您的程序成为 shell 函数,您可以添加自己的进程内命令,然后任何目录更改都会反映在 shell 进程中。
You need to convert your script to a shell function:
The reason is that each process has its own current directory, and when you execute a program from the shell it is run in a new process. The standard "cd", "pushd" and "popd" are builtin to the shell interpreter so that they affect the shell process.
By making your program a shell function, you are adding your own in-process command and then any directory change gets reflected in the shell process.
鉴于答案的不可读性和过于复杂性,我相信这是请求者应该做的事情,
PATH
中,运行。 scriptname
.
(点)将确保脚本不在子 shell 中运行。In light of the unreadability and overcomplication of answers, i believe this is what the requestor should do
PATH
. scriptname
The
.
(dot) will make sure the script is not run in a child shell.可以起一个别名
把上面的放在一起,如果不想写开头的“.”, 。 每一次
你想将你的脚本源到 shell 环境,
或者如果您只是不想记住必须这样做
以使脚本正常工作。
Putting the above together, you can make an alias
if you don't want to write the leading "." each time
you want to source your script to the shell environment,
or if you simply don't want to remember that must be done
for the script to work correctly.
如果您使用的是 bash,您可以尝试
在 .bashrc 文件中添加别名:添加此行:
当您在命令行上写入“p”时,它将更改目录。
If you are using bash you can try alias:
into the .bashrc file add this line:
when you write "p" on the command line, it will change the directory.
如果您运行 bash 脚本,那么它将在其当前环境或其子环境中运行,而不是在父环境中运行。
如果目标是运行您的命令:
goto.sh /home/测试
然后在 /home/test 中交互工作,一种方法是在脚本中运行 bash 交互式子 shell:
这样您将位于 /home/test 中,直到退出( exit 或 Ctrl+C )此 shell。
If you run a bash script then it will operates on its current environment or on those of its children, never on the parent.
If goal is to run your command :
goto.sh /home/test
Then work interactively in /home/test one way is to run a bash interactive subshell within your script :
This way you will be in /home/test until you exit ( exit or Ctrl+C ) of this shell.
使用pushd将当前目录推送到目录堆栈上并将其更改为给定目录,popd将目录置于堆栈顶部并更改为该目录。
With pushd the current directory is pushed on the directory stack and it is changed to the given directory, popd get the directory on top of the stack and changes then to it.
在 shell 脚本中添加以下 cd 行:
Add below cd line in your shellscript this:
只需转到
最后一行旁边添加此代码:
然后退出编辑器。
然后输入:
现在您可以在终端中使用:yourcommand
Simply go to
and add this code next to the last line:
Then quit editor.
Then type:
now you can use: yourcommand in terminal
我制作了一个更改目录的脚本。 看一下:https://github.com/ygpark/dj
I've made a script to change directory. take a look: https://github.com/ygpark/dj
基本上我们使用 cd.. 来从每个目录返回。 我想通过给出一次需要返回的目录数量来使其更容易。 您可以使用 alias 命令使用单独的脚本文件来实现这一点。 例如:
code.sh
在当前 shell 中使用
source code.sh
后,您可以使用 :从当前目录返回两步。 在此处详细解释。 那里还解释了如何将代码放入 ~/.bashrc 中,以便每个打开的新 shell 都会自动具有这个新的别名命令。 您可以通过添加更多
if 条件
和不同的参数修改代码来添加新命令以转到特定目录。 您还可以通过此处从 git 中提取代码。Basically we use
cd..
to come back from every directory. I thought to make it more easy by giving the number of directories with which you need to come back at a time. You can implement this using a separate script file using the alias command . For example:code.sh
After using
source code.sh
in the current shell you can use :to come two steps back from the current directory. Explained in detail over here. It is also explained over there how to put the code in ~/.bashrc so that every new shell opened will automatically have this new alias command. You can add new command to go to specific directories by modifying the code by adding more
if conditions
and different arguments. You can also pull the code from git over here.这是上述答案的简化汇编。
创建 shell 文件
shellfile.sh
在脚本中更改函数内的目录
现在运行脚本,前面带有
.
。.
使用当前线程/会话来执行脚本。This is a simplified compilation of above answer.
Create a shell file
shellfile.sh
In the script change your directory inside a function
Now run the script with
.
before it..
uses the current thread/session to execute the script.这是我目前对 bash 的做法(在 Debian 上测试过)。 也许有更好的方法:
文件 1:outer.sh
文件 2:inner.sh
This is my current way of doing it for bash (tested on Debian). Maybe there's a better way:
file 1: outer.sh
file 2: inner.sh
这种方法对我来说更容易。
假设在您是管理员的个人 iMac 上,在打开命令窗口时的默认目录 /Users/jdoe 下,这将是要转到的目录:/Users/jdoe/Desktop/Mongo/db.3.2.1 /垃圾桶。
这些是可以完成工作的步骤:
cd /Users/jdoe/Desktop/Mongo/db.3.2.1/bin
作为第一行。chmod 755 mongobin
源mongobin
pwd
瞧!
This approach is easier for me.
Suppose on a personal iMac where you are an admin, under the default directory when a command window is opened, /Users/jdoe, this will be the directory to go to: /Users/jdoe/Desktop/Mongo/db.3.2.1/bin.
These are the steps that can have the job done:
cd /Users/jdoe/Desktop/Mongo/db.3.2.1/bin
as the first line.chmod 755 mongobin
source mongobin
pwd
Voila!
我还创建了一个名为 goat 的实用程序,您可以使用它来更轻松地导航。
您可以在 GitHub 上查看源代码。
自 v2.3.1 起 使用概述 如下所示:
I've also created a utility called goat that you can use for easier navigation.
You can view the source code on GitHub.
As of v2.3.1 the usage overview looks like this: