bash 如何从特定目录执行命令而不实际更改到该目录

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

How do I execute a command from a specific directory without actually changing to that directory

bashscripting

提问by primeminister

I want to execute a command like 'git tag -l' inside a directory /home/user/git/app/ but I am actually in /home/user. How can I do that in bash without changing my working directory?

我想在目录 /home/user/git/app/ 中执行类似 'git tag -l' 的命令,但我实际上在 /home/user 中。如何在不更改工作目录的情况下在 bash 中执行此操作?

So NOT:

所以不是:

cd /home/user/git/app && git tag -l

because that actually changes my working directory and have to do 'cd /home/user' again.

因为这实际上改变了我的工作目录并且必须再次执行'cd /home/user'。

回答by Noufal Ibrahim

Just bracket the whole thing. That will run it in a subshell which can go to any directory and not affect your 'current working' one. Here's an example.

只是把整个事情括起来。这将在一个子shell 中运行它,该子shell 可以转到任何目录,而不会影响您的“当前工作”目录。这是一个例子。

noufal@sanctuary% pwd
/tmp/foo
noufal@sanctuary% (cd ../bar && pwd && ls -a )
/tmp/bar
./  ../
noufal@sanctuary% pwd
/tmp/foo
noufal@sanctuary%            

回答by Hai Vu

Here is another solution: use pushd to change directory, then popd to return:

这是另一种解决方案:使用 pushd 更改目录,然后使用 popd 返回:

pushd /home/user/git/app && git tag -l; popd

回答by Cascabel

If the command in question is always going to be a git command, you should just use the --git-dirand --work-treeoptions to tell git what to do! (Or if you're doing this a lot over the course of a script, set the variables GIT_DIR and GIT_WORK_TREE to the appropriate paths)

如果有问题的命令总是一个 git 命令,你应该只使用--git-dir--work-tree选项来告诉 git 做什么!(或者,如果您在脚本过程中经常这样做,请将变量 GIT_DIR 和 GIT_WORK_TREE 设置为适当的路径)

If this is a general question, I believe Andrzej has a start on the best suggestion: use a subshell. The proper way to start a subshell, though, is to use parentheses, not to use command substitution (unless you actually want to capture the output):

如果这是一个普遍的问题,我相信 Andrzej 有一个最好的建议:使用子shell。但是,启动子shell的正确方法是使用括号,而不是使用命令替换(除非您确实想要捕获输出):

( cd $dir && run_command )

The other solution, as suggested by Felix and ibread, will of course work, but do be careful - if the command you're executing is perhaps a shell function, then it could also cd, and change the effect of the cd -at the end. The safest thing in the general case is to store the current directory in a variable first.

另一种解决方案,如 Felix 和 ibread 所建议的,当然可以工作,但要小心——如果你正在执行的命令可能是一个 shell 函数,那么它也可以 cd,并改变最后的效果cd -。一般情况下最安全的做法是先将当前目录存储在变量中。

回答by D.Shawley

You might want to do something like (cd /home/user/git/app && git tag -l). This spawns a new shell and executes the commands in the shell without changing your shell. You can verify this by executing the following:

你可能想要做类似的事情(cd /home/user/git/app && git tag -l)。这会产生一个新的 shell 并在不更改 shell 的情况下执行 shell 中的命令。您可以通过执行以下操作来验证这一点:

$ echo $OLDPWD
/Users/daveshawley
$ (cd / && ls)
...
$ echo $OLDPWD
/Users/daveshawley

回答by ibread

try to use

尝试使用

cd -

after everything is done. This command is used to go back to your last working directory.

一切都完成后。此命令用于返回上一个工作目录。

回答by Fillipos Christou

The following function can be added in the .bashrc

可以在.bashrc中添加以下功能

execute_under_directory()
{
    if [ $# -lt 2 ] 
    then
        echo "usage: execute_under_directory <DIRECTORY_PATH> <COMMAND>"
        return 1
    fi

    local current_directory=$(pwd)
    cd 

    shift
    "$@"

    cd $current_directory
}

And by also adding an alias, for example

并通过添加别名,例如

alias eud=execute_under_directory

, you can run any command just like this:

,你可以像这样运行任何命令:

eud path/to/project git tag -l