Reputation: 333
Take the bash script wtf.sh
#!/bin/bash
echo $1
echo $2
echo $3
./wtf.sh 1 2 3
1
2
3
./wtf.sh * * *
end
./wtf.sh 1 * *
1
wtf.sh
wtf.sh
./wtf.sh 1 "*" "*"
1
wtf.sh
wtf.sh
I'm aware that *
is special parameter in Bash but does that mean it's impossible to pass the *
by itself as a literal character from the command line? What I was expecting was
./wtf.sh * * *
*
*
*
How do I accomplish this?
Upvotes: 4
Views: 3833
Reputation: 997
If you are passing *
as a value of an command line option and extracting the command line option using getopt
then you can use -S*
, where -S is command line option and *
is its value. Please note that there is no space between S
and *
. If you give space then path expansion happens automatically.
Upvotes: 0
Reputation: 241691
You can pass a literal * by:
Putting it in quotes: "*"
Putting it in apostrophes: '*'
Backslash escaping it: \*
Those are all equivalent (in this case).
The reason that didn't work is that your script has a problem: when you don't quote $1
(and $2
and $3
), you are telling the shell to perform pathname expansion and word splitting on their contents. That's actually almost never what you want -- and it clearly isn't what you want in this case. So your script should read:
#!/bin/bash
echo "$1"
echo "$2"
echo "$3"
Here, the arguments must be surround with double quotes ("$1"
), which allows parameter expansion to take place, but inhibits pathname expansion and word splitting.
Upvotes: 7