0

I am trying to pass a quoted globbing pattern to find command in script using variable. Does not work without eval. Is it a way to do this without eval?

First I use the followin command:

find . -name '*a*'

It works OK and produces the following output:

./AaA
./dir1/aaa.tst
./dir1/zabc1122333.tst
./dir2/dir3/zabc1122333.tst
./yyy/AaA
./zabc1122333.tst

Now I want to use a variable in place of quoted glob pattern 'a'.

This does not work:

A='*a*' ; find . -name $A

It produces:

find: zabc1122333.tst: unknown primary or operator

The following four commands do not work either. They produce nothing:

A="'*a*'" ; find . -name $A

A=\'\*a\*\' ; find . -name $A

A=\'*a*\' ; find . -name $A

A='\*a\*' ; find . -name $A

Finally it works with eval:

A=\'\*a\*\' ; eval find . -name $A

./AaA
./dir1/aaa.tst
./dir1/zabc1122333.tst
./dir2/dir3/zabc1122333.tst
./yyy/AaA
./zabc1122333.tst

Is it possible to do it without eval?

1 Answers1

6

Use quotes (") to prevent bash from globbing.

A='*a*'; find . -name "$A"

Look at the output of the following two commands.

A='*a*'; echo find . -name $A

and

A='*a*'; echo find . -name "$A"
Cyrus
  • 84,225
  • 14
  • 89
  • 153