How do I expand commands within a bash alias?

血红的双手。 提交于 2019-12-12 07:22:44

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/23507074/how-do-i-expand-commands-within-a-bash-alias

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!