Reputation:
I'm new to bash and have encountered a problem i can't solve. The issue is i need to use find -name with a name defined as a variable. Part of the script:
read MYNAME
find -name $MYNAME
But when i run the script, type in '*sh' for read, there are 0 results. However, if i type directly in the terminal:
find -name '*sh'
it's working fine.
I also tried
read MYNAME
find -name \'$MYNAME\'
with typing *sh for read and no success.
Can anyone help me out?
Upvotes: 3
Views: 8894
Reputation: 17916
You probably want
find -name "$MYNAME"
since this prevents $MYNAME
from being subject to bash's pathname expansion (a.k.a. "globbing"), resulting in *sh
being passed intact to find
. One key difference is that globbing will not match hidden files such as .ssh
, whereas find -name "*sh"
will. However since you don't define the expected behaviour you are seeking, it's hard to say what you need for sure.
Upvotes: 3
Reputation: 10357
Most probably
read MYNAME
find -name "$MYNAME"
is the version you are looking for. Without the double quotes "
the shell will expand *
in your *sh
example prior to running find
that's why your first attempt didn't work
Upvotes: 4