0

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

mxbrew
  • 433
  • 4
  • 3
  • 2
    Use `"$*"` instead of `"$1"` in the function. See [Positional Parameters](http://www.gnu.org/software/bash/manual/bashref.html#Positional-Parameters). – Etan Reisner Feb 02 '15 at 15:46
  • That worked, thanks! http://d.pr/i/10wgo/F1z2FPXo – mxbrew Feb 02 '15 at 15:49
  • With `"Commit Message"`, `"$1"` would have been fine; it's unquoted `$1` that was the problem. – Charles Duffy Feb 02 '15 at 16:21
  • By the way, the `function` keyword is best avoided -- just `push() {` is enough to define a function, and that way your function will work in any POSIX shell rather than being specific to shells with bash extensions. – Charles Duffy Feb 02 '15 at 16:23

1 Answers1

1

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.

Community
  • 1
  • 1
mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
  • 2
    `"$1"` would have worked just as well, for the single-argument case, and would preserve the ability to pass additional arguments. It's unquoted `$1` that was the problem. – Charles Duffy Feb 02 '15 at 16:22