mxbrew
mxbrew

Reputation: 433

Catching String as Argument in Bash

I have a script I use for Git

function push() {
  git add --all
  git commit -m $1
  git push
}

Which I invoke by

p "Commit message"

My question is, how can I pass everything after p as one argument, essentially passing everything after p as the commit message - importantly - I want to know if there is a way to do this without quotation marks

Upvotes: 0

Views: 60

Answers (1)

mgarciaisaia
mgarciaisaia

Reputation: 15630

If that's the only argument you have, you can use $* in your script to get all the parameters. Also, quote them:

function push() {
    git add --all
    git commit -m "$*"
    git push
 }

You can use "$1", too, leaving place for more parameters, but in that case you must use quotes while invoking the function (say, $ push "Some message" instead of just $ push Some message). It's more correct, but for this function purposes - just to economise time - it may be more useful.

Anyway, I don't think it's a good idea to have such a function. git add is a good moment to review your commit, decide what to include in it and what to leave for other commits, and to construct a really nice commit message. Plus, you would often want to delay the push until you have a couple of commits.

I recommend you to use something like tig for easily selecting what to stage and the like.

Upvotes: 1

Related Questions