3

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?

  • It would help if you gave examples of files which you expect to match (i.e. define "working fine"), and also specify whether you are executing or `source`-ing the script. By the way, it's conventional to use lowercase for non-constant variables, not uppercase. – Adam Spiers Mar 20 '13 at 01:33

2 Answers2

4

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

mikyra
  • 10,077
  • 1
  • 40
  • 41
3

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.

Adam Spiers
  • 17,397
  • 5
  • 46
  • 65