I would like to create a simple bash function to use for my convenience. Following the answer given at: Joining bash arguments into single string with spaces I've been able to mash up this small piece of code:

function gcm {
  eval "git commit -m ${msg}"

Now, this example is very convenient for commit messages like "Hello, it's me" (simple set of word characters that is), but when I wan't a commit message like: "[WIP] Halfway trough code.", I get an error message as follows: zsh: no matches found: [WIP]

Would you please clarify for me what is happening in the background and why this snippet fails?

  • 1
    Why eval? Just do git commit directly, double quote $msg, the problem gone – cuonglm Apr 4 '16 at 19:04
  • If you want to use square brackets in your strings, you might want to see the instructions on THIS PAGE and THIS PAGE. I am not very familiar with zsh. – MelBurslan Apr 4 '16 at 19:05

ZSH is delightfully free of the word-splitting behaviour seen in other shells (unless for some bizarre reason the SH_WORD_SPLIT option has been turned on), so there is no need to use strange double-quoting constructs.

% (){ print -l $* } a b c  
% (){ print -l "$*" } a b c
a b c
% (){ local msg; msg="$*"; print -l $msg } a b c
a b c

Thus, the following should suffice:

function gcm {
  local msg
  git commit -m $msg

Globbing may be disabled by quoting strings like [WIP] as '[WIP]', or perhaps via a noglob alias:

% function blah { print -l "$*" }
% alias blah='noglob blah'
% blah [de] *blah*
[de] *blah*
  • Very good. Almost done here. After following your advice, the snippet looks as follows: function gcm { local msg msg="$*" git commit -m $msg } alias gcm ='noglob gcm' This time, the error: noglob gcm not found Pardon me for the poor formating, new to the forum =) – 33windowlicker Apr 5 '16 at 8:44
  • @33windowlicker When defining an alias there must not be any white spaces surrounding the =. So it needs to be alias gcm='noglob gcm'. – Adaephon Apr 6 '16 at 16:00
  • Very good, marking this as solved. – 33windowlicker Apr 7 '16 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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