Simple scripting: Prevent an "*" in a variable from resolving during runtime

Hopefully this is a simple question.  It was triggered while I was trying to help someone ELSE in EE with scripting.

Is there a way to stop a variable with an “*” in it from resolving the “*” at runtime?

I.E.
In the script called test2...
************
#!/bin/ksh
for var in $*
do
 echo $var
done
**********

...I get the following results if I run ...
ksh # ./test2 a b "test" d
a
b
test
d

...which is what I want.  HOWEVER, if I run the script with the following parameters ...

ksh # ./test2 a b "*test*" d
a
b
test
test1,ksh
test2
test2.ksh
test3
d

...I get a listing of all the “test” files in the dir...instead of it listing “*test*” WITH the asterisks, like follows ...
a
b
*test*
d

How do I get it to echo (or grep or whatever) the “*test*” WITH the asterisks, instead of functionally resolving them?

Many Thanks!
M
LVL 3
Mike R.Asked:
Who is Participating?
 
TintinCommented:
You need to disable shell globbing.

This depends on what shell you are running.  For ksh/bash, you do:

set -o noglob

at the command prompt.

0
 
Mike R.Author Commented:
can that be done within the script (so it affects on the child shell being generated by the script?)
0
 
Mike R.Author Commented:
Nevermind...a quick shell test answered the question to "yes".

Thanks!  I new it would be something easy :-)!

M
0
 
Mike R.Author Commented:
By the way...I think it was the "multigrep" question that you answered, that inspired the question in the first place :-D

Thanks again!
M
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.