shigg
shigg

Reputation: 802

How to pass a sentence between double quotes in bash when creating a function

I am trying to create an alias which is a shorthand for git commit -m "commit message".

What I was trying to do is create a function below in ~/.aliases.

gc()
{
    git commit -m ""$@""
}

This give me this error message when I use this "alias" gc install project

error: pathspec 'install' did not match any file(s) known to git.
error: pathspec 'project' did not match any file(s) known to git.

while I was expecting it to be git commit -m "install project"

How can I make this alias work as I want it to?

Upvotes: 1

Views: 109

Answers (1)

chepner
chepner

Reputation: 530970

Since the goal is to combine all the arguments to gc into a single string to use as the argument to -m, you want to use $*, not $@. Further, you don't need to specify the quotes. In git commit -m "install project", the quotes are not part of the argument; they are just there to instruct bash that install project is a single word, not two separate words, to pass as an argument to git.

gc () {
    git commit -m "$*"
}

However, I would encourage you to take responsibility for passing a single, properly quoted argument to gc, so that you don't have to worry about what the shell will do to characters like $, *, etc.

gc () {
    git commit -m "$1"
}

gc "install my *awesome* project"

Upvotes: 2

Related Questions