Home > other >  echo arbitrary arguments in git
echo arbitrary arguments in git

Time:01-28

I am chaining a bunch of git commands in alias as below. How do I get the 'echo' part to work:

[alias]
        comb = ! sh -c 'echo \"Combining branches $1 with $2\"' && git checkout $2 && git merge $1 && git push && git checkout $1 && :

Some context: Git Alias - Multiple Commands and Parameters

CodePudding user response:

Do not use single quotes around echo — Unix shells do not expand parameters inside single quotes. The fix is

$ git config alias.comb '! sh -c "echo \"Combining branches $1 with $2\""'
$ git config alias.comb 
! sh -c "echo \"Combining branches $1 with $2\""

Example:

$ git comb 1 2 3                
Combining branches 1 with 2

Or

$ git config alias.comb '! sh -c "echo \"Combining branches $*\""'
$ git config alias.comb         
! sh -c "echo \"Combining branches $*\""
$ git comb 1 2 3                
Combining branches 1 2 3

CodePudding user response:

The standard trick is to define a function which you immediately call.

[alias]
        comb = ! f () { echo "Combining branches $1 with $2" && git checkout "$2" && git merge "$1" && git push && git checkout "$1" && :; } f

This simplifies the quoting.

CodePudding user response:

Your quotes are messed up:

        comb = ! sh -c 'echo "Combining branches $1 with $2" && git checkout "$2" && git merge "$1" && git push && git checkout "$1"'

You could also consider changing your alias to an executable called git-comb and store it somewhere in your path:

$ cat /path/to/executables/git-comb
#!/bin/sh
if [ "$#" -ne 2 ]
then
  >&2 printf 'Usage: %s <ref> <ref>\n" "${0##*/}"
  exit 2
fi
echo "Combining branches $1 with $2"
git checkout "$2"
git merge "$1"
git push
git checkout "$1"

This way you can call it with:

$ git comb branch_1 branch_2
  •  Tags:  
  • Related