Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  12] [ 0]  / answers: 1 / hits: 33820  / 2 Years ago, sat, october 15, 2022, 2:14:25

I often have to do a recursive search in files. Tired of typing the whole "find/grep" combination all the time, I just created a script with the following line:



find . -name $1 -exec grep $2 {} + 2>/dev/null


The idea is that I should be able to run it as, for example:



myfind '*' hello


The problem is, when '*' gets passed into the script, the script is expanding it to each file in the directory.



I tried putting a quote around $1:



 find . -name '$1' -exec grep $2 {} + 2>/dev/null


However, this turns $1 into a literal string.



Would appreciate your help in finding the right syntax. Must be something simple that I am missing.


More From » bash

 Answers
4

I've demonstrated the problem here:



$ pie() { echo $1; }; pie '*'
1 2 3 4 5 file


Expanded. Bother.



But the solution is quite simple. You just need to quote it but in a way that bash will understand. We use double-quotes. It will be replaced with the variable contents but it won't be expanded.



$ pie() { echo "$1"; }; pie '*'
*

[#24205] Sunday, October 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
corsee

Total Points: 479
Total Questions: 122
Total Answers: 106

Location: Barbados
Member since Sat, May 9, 2020
4 Years ago
;