ZSH alias with parameter

albttx picture albttx · Dec 17, 2015 · Viewed 52.3k times · Source

I am trying to make an alias with parameter for my simple git add/commit/push.

I've seen Function could be used as alias so i try but i didn't make it ..

before i had:

alias gitall="git add . ; git commit -m 'update' ; git push"

But i want to be able to modify my commits :

function gitall() {
    "git add ."
    if [$1 != ""]
        "git commit -m $1"
    else
        "git commit -m 'update'"
    fi
    "git push"
}

(i know it's a terrible git practice)

Answer

Kevin picture Kevin · Dec 17, 2015

You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].

gitall() {
    git add .
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
}

*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.