Buczo
Buczo

Reputation: 9

passing a glob expression given as a script argument to find

i want find some files with given arguments in command line, this is my script

find -name $1

when i run this with argument '*.txt' it doesn't work. Why? What I can fix it?

Upvotes: 0

Views: 185

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295687

An unquoted expansion of a wildcard is replaced with a list of filenames in the current directory that match the pattern.

Thus:

set -- '*.txt' # same as running your script with '*.txt' as first argument
touch a.txt b.txt c.txt

find -name $1

...will run:

find -name a.txt b.txt c.txt

...which isn't valid usage.


To avoid glob expansion, always quote your expansions: find . -name "$1"

Upvotes: 2

Related Questions