1

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 {
  msg="'$*'"
  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 04 '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 04 '16 at 19:05
  • I pesonnally just have a alias gc='git commit -m "' I just type gc, my message and end it by typing a double quote. Unlike the function, it allows for multiline commit messages – cassepipe Jun 27 '23 at 22:06

1 Answers1

4

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  
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
  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*
% 
thrig
  • 34,938
  • 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 =)

    – user3223162 Apr 05 '16 at 08: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 06 '16 at 16:00
  • Wow, I didn't know zsh had immediately called functions expressions ! – cassepipe Jun 27 '23 at 22:03