问题描述:

I usually do this:

git commit -m "My hands are typing words!"

I am gettin' tired of that, so I made this batch:

@echo off

set var=%*

git commit -m "%var%"

Which works as:

commit.bat blah blah blah

So I can drop the -m and the quotes, but it adds .bat. When I remove file extension, git Bash tries to interpret the batch as bash. So I need to use bash syntax instead. That's fine, I tried this:

#!/usr/bin/bash

git commit -m "[email protected]"

That doesn't work, it passes arguments as multiple arguments. It invokes this:

git commit -m My hands are typing words!

I tried to add even more quotes (git commit -m ""[email protected]""), no effect.

So how do I convert all arguments to a string that can be passed in bash to another command?

网友答案:

You can use "$*" instead of "[email protected]".
This sample script should explain it:

$ cat a.sh
#!/bin/bash
echo '$*:'
printf "%s\n" $*
echo
echo '"$*":'
printf "%s\n" "$*"
echo
echo '[email protected]:'
printf "%s\n" [email protected]
echo
echo '"[email protected]":'
printf "%s\n" "[email protected]"

$ ./a.sh a b "c    d"             e
$*:
a
b
c
d
e

"$*":
a b c    d e

[email protected]:
a
b
c
d
e

"[email protected]":
a
b
c    d
e

But correctly, you should use "$1" & quote your message string before passing to the script.
In above example, you can see that the spaces between c & d are retained, but those between "c d" & e are lost.

相关阅读:
Top