0

I have this situation at hand:

simple_git_push(){


    export cm_args="${@}"

  (

    set -eo pipefail;

    commit_msg="$(echo "$cm_args" |  tr -d '[:space:]')" # trim whitespace

    if [[ -z "$commit_msg" ]]; then
       commit_msg='squash-me'
    fi
 
    git commit -am "$commit_msg" # ...
  )

}

the problem is the git commit message is always the first token, for example if I do this:

simple_git_push foo bar baz

the commit message will be "foo", but I want the commit message to be 'foo bar baz', how can I do this? Bonus points for escaping characters that git wont like in commit messages..

1
  • Actually, this code makes the commit message foobarbaz (no spaces, tr removes them). Are you sure this is exactly the code that is executed?
    – fra-san
    Aug 3, 2020 at 20:04

1 Answer 1

1

I would go with

simple_git_push(){
    export cm_args=("$@")   # assuming I really wanted to export args
    git commit -am "${*:-squash-me}"
}

but I would suggest not bothering with the export line.

This is one of the rare times that $* is better than $@.

3
  • i want to have the subshell, as in OP, so does this work? export cm_args is so that it gets passed to subshell. So using $* I dont think will work for me? Aug 3, 2020 at 21:01
  • Did you try the code that @icarus provided?
    – John1024
    Aug 3, 2020 at 22:44
  • 1
    "i want to have the subshell" - any particular reason? "export cm_args is so that it gets passed to subshell" - whilst this may not be covered in elementary shell scripting guides, there is a difference between how how a subshell is started and how an external command is started, so you don't need the export. "So using $* I don't think will work for me?", as @John1024 says, you could have tried it.
    – icarus
    Aug 3, 2020 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .