Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.