Reputation: 1189
so i have a line of code like:
list=$(find . -maxdepth 1 -name "$1" -print0 | sed 's,\.\/,,g')
echo $list
so in terminal when i do bash script_name string
i hope it will display all the files that contain "string"
Now in a folder i have 4 matches : TesT1.c TesT1.h TesT2.c TesT2.h
when i do
bash script_name TesT*
my code only return the first match ,which is TesT1.c
Where did i do wrong, thank you in advance
Upvotes: 0
Views: 216
Reputation: 81032
When you run bash script_name TesT*
your current shell expands the glob so your run command is actually bash script_name TesT1.c TesT1.h TesT2.c TesT2.h
.
Your script then uses $1
(the first argument) and runs
find . -maxdepth 1 -name "TesT1.c" -print0 | sed 's,\.\/,,g'
which obviously only finds itself.
Quote the glob on the initial command line.
bash script_name 'TesT*'
Also there's no reason to stuff the output from find
into a variable and then echo it back out like that.
Upvotes: 0
Reputation: 786001
When you do:
bash script_name TesT*
bash expands TesT*
at the time of calling your script and makes it:
bash script_name TesT1.c TesT1.h TesT2.c TesT2.h
Since you're only using $1
you just get: TesT1.c
You need to call you script as
bash script_name 'TesT*'
to avoid expansion of glob pattern (due to use of single quote)
Upvotes: 1