How do I expand commands within a bash alias?

2020-03-07 07:07发布

I'd like to create an alias that runs git pull origin <current branch name>

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

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

I've tried

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

and

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.

标签: git bash alias
1条回答
Anthone
2楼-- · 2020-03-07 07:21

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

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)
}
查看更多
登录 后发表回答