Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I saw the following shortcut for "git commit" suggested somewhere:

function gc {
  git commit -m "$*"

This is supposed to allow you to write the commit message inline in the shell, without quotes or anything. Like:

gc This is a commit message

The problem is, it doesn't seem to work if the commit message itself contains brackets or quotes. (And I use a lot of brackets/quotes in my commit messages.) Can anyone well versed in the art of bash scripting suggest a better shortcut?

share|improve this question
up vote 3 down vote accepted

I don't think this is a problem with your function (or, more accurately, I don't think this is a problem you can solve in your function). bash parses the command line (including interpreting quoted strings, various bracket expressions, etc) before it calls your function -- before it even decides to call your function. So when you type something like gc fixed Greg's bug, bash will require that you close the quoted string before it executes the function; when you type gc printf("%s", integervar) not a good idea, bash will complain about the parentheses and never even get to the point of deciding what command/function/whatever was being requested.

I presume the point of using $* was to avoid having to quote the memo on the command line, but that only avoids having to quote spaces in the message. If the message contains other shell metacharacters, you must quote or escape them appropriately:

gc "fixed Greg's bug"
gc 'printf("%s", integervar) not a good idea'
gc 'fixed this & deferred that'
share|improve this answer
OK, understood. Thanks for explaining! – Alex D Feb 15 '13 at 5:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .