Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • you should export that SearchTerm variable, if you want to pass it to child process. Instead of local SearchTerm=...;, write SearchTerm=$1 find .... Commented Jan 22, 2020 at 0:17
  • notice that you do not need to quote the $1 in things like func(){ VAR=$1 find ... ; } Commented Jan 22, 2020 at 0:20
  • And even if you pass it to the subshell, passing ${d}/$... still won't work, because the shell does not expand variables recursively. Commented Jan 22, 2020 at 0:28
  • @mosvy Yes exporting doesn't help. How can we make this work? I want to generalize this function by passing different SearchTerm while invocation of the function. Also, may I ask you why we don't need to quote $1 as you wrote in the previous comment. Commented Jan 22, 2020 at 8:01
  • You don't need to quote it because split+glob doesn't happen on the right side of a variable assignment; example: f(){ v=$1 printenv v; }; f '* *'. I think that @Philippes' solution, which could be simplified to just FIND(){ find ... sh -c '... [ -f "'"$1"'" ] && ...' ...; } is fine for what you want to achieve, and trying to "improve" it (with eg. backquoting "s in the interpolated variable) will only break it. Commented Jan 22, 2020 at 14:17