ZSH alias with parameter

I used this function in .zshrc file:

function gitall() {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update # default commit message is `update`
    fi # closing statement of if-else block
    git push origin HEAD
}

Here git push origin HEAD is responsible to push your current branch on remote.

From command prompt run this command: gitall "commit message goes here"

If we just run gitall without any commit message then the commit message will be update as the function said.


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.


If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:

alias example='f() { echo Your arg was $1. };f'

I see this approach used a lot in .gitconfig aliases.