如何在bash别名中扩展命令? [英] How do I expand commands within a bash alias?

查看:77
本文介绍了如何在bash别名中扩展命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个运行 git pull origin<当前分支名称>

我可以使用 git rev-parse --abbrev-ref HEAD 来获取当前的分支名称。

I can get the current branch name with git rev-parse --abbrev-ref HEAD.

但是怎么办我将第二个命令放在bash别名的第一个命令中?

But how do I put that 2nd command within the first in a bash alias?

我尝试过

alias gup="git pull origin `git rev-parse --abbrev-ref HEAD`"

alias gup="git pull origin $(git rev-parse --abbrev-ref HEAD)"

,但两者都导致别名中的初始分支名称,而不是当时的分支名称

but both result in the initial branch name in the alias, not the branch name at the time the command is run.

推荐答案

直接的问题是,当别名为时,命令分站会在双引号内展开定义。使用单引号将通过延迟命令替换直到您实际使用别名来解决此问题。

The immediate problem is that the command substations are expanded inside the double quotes when the alias is defined. Using single quotes will fix that by deferring the command substitution until you actually use the alias.

alias gup='git pull origin $(git rev-parse --abbrev-ref HEAD)'

但是,您可以省去尝试使用shell函数来使报价正确的麻烦。

However, you can save yourself the trouble of trying to get the quoting just right by using a shell function instead.

gup () {
    git pull origin $(git rev-parse --abbrev-ref HEAD)
}

这篇关于如何在bash别名中扩展命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆