Reputation: 83
myScript.sh:
grep --color -rn --include=*."$1" "$2" "$3"
command:
./myScript.sh java keyword . # it works!
./myScript.sh java,xml keyword . # it doesn't..
./myScript.sh {java,xml} keyword . # it doesn't..
./myScript.sh "{java,xml}" keyword . # it doesn't..
./myScript.sh '{java,xml}' keyword . # it doesn't..
grep -rn --include=*.{java,xml} keyword . # of course it works
How do I command? or how do I edit myScript so that it can work?
Upvotes: 2
Views: 196
Reputation: 198436
In your examples, {java,xml}
argument gets expanded by your shell into two parameters java xml
before it hits your script (which makes your $1
just java
, and messes up your parameter numbering with $2
being xml
, $3
being keyword
).
In my hurried and untested first attempt at answering, I forgot one crucial bit: brace expansion goes first, before variable expansion, so when your variable $1
is substituted, the brace doesn't get another look.
The only way I found to get around that is using eval
:
eval grep --color -rn --include=*."$1" "$2" "$3"
and calling it with
./myScript.sh '{java,xml}' keyword .
Or, simplifying a bit,
eval grep --color -rn --include=*."{$1}" "$2" "$3"
and calling with
./myScript.sh java,xml keyword .
Upvotes: 1