Reputation: 73
I need to pass argument for commit function. When I do the commit through
./test.sh commit -m "first"
its not really committing it. believe somehow I am not passing right argument parameter either in case or function.
Here is the script
#!/usr/bin/env bash
clone () {
git clone $1
}
commit () {
git commit $*
}
case $1
in
clone) clone $2 ;;
commit) commit $2 ;;
*) echo "Invalid Argument passed" ;;
esac
Upvotes: 0
Views: 1803
Reputation: 8064
To safely support multiple arguments (including ones with special characters) the function bodies should be
git clone "$@"
and
git commit "$@"
.
For the same reasons, the case
code should be:
case $1 in
clone) clone "${@:2}" ;;
commit) commit "${@:2}" ;;
*) echo "Invalid Argument passed" ;;
esac
In the functions, "$@"
expands to all the function arguments, safely quoted so they are not subject to word splitting or expansions.
In the case
statement, ${@:2}
expands to the list of command line arguments after the first one, safely quoted.
For more information see Handling positional parameters [Bash Hackers Wiki].
Upvotes: 1
Reputation: 8601
The arguments are processed like this by bash:
./test.sh commit -m "first"
0: ./test.sh
1: commit
2: -m
3: first
So your "first"
is actually argument $3
.
Upvotes: 1