Reputation: 408
I'm trying to create a function that creates a git commit.
function commit_thing {
git add $1
shift
git commit $@
}
I can't quote $@, because I'm trying to retain the ability to create both a subject and a body for a commit. For example:
commit_thing thing1.txt -m "This is a subject" -m "This is a detailed body"
How should I pass the remaining variables in such a way as to quote the ones originally passed in quotes to the function, while not quoting the flags that need to be passed to the git commit command?
Upvotes: 0
Views: 86
Reputation: 532303
The whole purpose of $@
is to allow individual arguments to remain quoted yet distinct. Compare
$ set -- "a b" "c d"
$ printf '%s\n' "$*"
a b c d
$ print '%s\n' "$@"
a b
c d
So you would simply write
function commit_thing {
git add "$1"
shift
git commit "$@"
}
With a command like commit_thing thing1.txt -m "This is a subject" -m "This is a detailed body"
, the 5 arguments are thing1.txt
, -m
, This is a subject
, -m
, and This is a detailed body
. After the shift, "$@"
will pass the remaining arguments as-is to git commit
as separate arguments, not as one single argument as "$*"
would.
Upvotes: 1