从 Bash 脚本更改当前目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/874452/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:11:31  来源:igfitidea点击:

Change the current directory from a Bash script

bashenvironment

提问by artemb

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:

我想在 Bash 中创建一个用于目录导航的实用程序。我创建了一个如下所示的测试脚本:

#!/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?

当我从 Bash shell 执行脚本时,当前目录不会改变。是否有可能从脚本更改当前的 shell 目录?

采纳答案by winden

You need to convert your script to a shell function:

您需要将脚本转换为 shell 函数:

#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
#   . ~/bin/myprog.sh
#

function myprog() {
  A=
  B=
  echo "aaa ${A} bbb ${B} ccc"
  cd /proc
}

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.

原因是每个进程都有自己的当前目录,当你从 shell 执行一个程序时,它会在一个新进程中运行。标准的 "cd"、"pushd" 和 "popd" 内置于 shell 解释器中,因此它们会影响 shell 进程。

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.

通过使您的程序成为 shell 函数,您将添加自己的进程内命令,然后任何目录更改都会反映在 shell 进程中。

回答by Norbert Hartl

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:

相反,您可以像这样启动脚本:

. myscript.sh

The .will evaluate the script in the current environment, so it might be altered

.会在目前的环境评估脚本,所以它可能被改变

回答by winden

In light of the unreadability and overcomplication of answers, i believe this is what the requestor should do

鉴于答案的不可读性和过于复杂,我认为这是请求者应该做的

  1. add that script to the PATH
  2. run the script as . scriptname
  1. 将该脚本添加到 PATH
  2. 运行脚本 . scriptname

The .(dot) will make sure the script is not run in a child shell.

.(点)将确保该脚本没有在子shell中运行。

回答by MergerMan

Putting the above together, you can make an alias

把上面的放在一起,你可以做一个别名

alias your_cmd=". your_cmd"

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.

如果你不想写前导“.” 每次您想将脚本提供给 shell 环境时,或者如果您只是不想记住必须这样做才能使脚本正常工作。

回答by akarca

If you are using bash you can try alias:

如果您使用 bash,您可以尝试别名:

into the .bashrc file add this line:

在 .bashrc 文件中添加这一行:

alias p='cd /home/serdar/my_new_folder/path/'

when you write "p" on the command line, it will change the directory.

当你在命令行上写“p”时,它会改变目录。

回答by philippe lhardy

If you run a bash script then it will operates on its current environment or on those of its children, never on the parent.

如果您运行 bash 脚本,那么它将在其当前环境或其子级环境中运行,而不是在父级上运行。

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 :

如果目标是运行您的命令:goto.sh /home/test 然后在 /home/test 中交互工作,一种方法是在您的脚本中运行 bash 交互式子shell:

#!/bin/bash
cd 
exec bash

This way you will be in /home/test until you exit ( exit or Ctrl+C ) of this shell.

这样,您将在 /home/test 中,直到您退出( exit 或 Ctrl+C )此 shell。

回答by seb

With pushdthe current directory is pushed on the directory stack and it is changed to the given directory, popdget the directory on top of the stack and changes then to it.

使用pushd将当前目录推送到目录堆栈上,并将其更改为给定目录,popd获取堆栈顶部的目录,然后更改为该目录。

pushd ../new/dir > /dev/null
# do something in ../new/dir
popd > /dev/null

回答by ThangTD

Simply go to

只需转到

yourusername/.bashrc (or yourusername/.bash_profile on MAC) by an editor

and add this code next to the last line:

并在最后一行旁边添加此代码:

alias yourcommand="cd /the_path_you_wish"

Then quit editor.

然后退出编辑器。

Then type:

然后输入:

source ~/.bashrc or source ~/.bash_profile on MAC.

now you can use: yourcommandin terminal

现在您可以在终端中使用:yourcommand

回答by Joel

I've made a script to change directory. take a look: https://github.com/ygpark/dj

我已经制作了一个脚本来更改目录。看一看:https: //github.com/ygpark/dj

回答by Arun Chettoor

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:

基本上我们使用cd..从每个目录回来。我认为通过提供您一次需要返回的目录数量来使其更容易。您可以使用 alias 命令使用单独的脚本文件来实现这一点。例如:

code.sh

代码.sh

#!/bin/sh
 _backfunc(){
 if [ "" -eq 1 ]; then
  cd ..
 elif [ "" -eq 2 ]; then
  cd ../..
 elif [ "" -eq 3 ]; then
  cd ../../..
 elif [ "" -eq 4 ]; then
  cd ../../../..
 elif ["" -eq 10]; then
  cd /home/arun/Documents/work
 fi
 }
alias back='_backfunc'   

After using source code.shin the current shell you can use :

source code.sh当前 shell 中使用后,您可以使用:

$back 2 

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 conditionsand different arguments. You can also pull the code from git over here.

从当前目录返回两步。在这里详细解释。那里还解释了如何将代码放在 ~/.bashrc 中,以便每个新打开的 shell 都会自动拥有这个新的别名命令。您可以通过添加更多if conditions不同的参数修改代码来添加新命令以转到特定目录。你也可以从 git 中提取代码到这里