ZSH, concatenate passed in arguments into a single string

5,600

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*
% 
Share:
5,600

Related videos on Youtube

33windowlicker
Author by

33windowlicker

Updated on September 18, 2022

Comments

  • 33windowlicker
    33windowlicker over 1 year

    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?

    • cuonglm
      cuonglm about 8 years
      Why eval? Just do git commit directly, double quote $msg, the problem gone
  • 33windowlicker
    33windowlicker about 8 years
    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 =)
  • Adaephon
    Adaephon about 8 years
    @33windowlicker When defining an alias there must not be any white spaces surrounding the =. So it needs to be alias gcm='noglob gcm'.
  • 33windowlicker
    33windowlicker about 8 years
    Very good, marking this as solved.