help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Passing multiple search directories to grep


From: hancooper
Subject: Passing multiple search directories to grep
Date: Tue, 03 Aug 2021 12:10:21 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, August 3, 2021 11:32 AM, Greg Wooledge <greg@wooledge.org> wrote:

> On Tue, Aug 03, 2021 at 10:59:38AM +0000, hancooper via wrote:
>
> > Is grep able to take multiple search directories stored in an array like so
> > grep -rl "$ptrn" ${dirlist[@]} |
>
> You need quotes. "${dirlist[@]}"
>
> This will pass the elements of the array as arguments to grep. What
> your version of grep does with them after that is up to grep.
>
> Remember, the shell constructs a command as a list of strings, and
> passes them off to the kernel to execute. grep never sees an "array".
> It only sees the expanded list of argument strings.
>
> So, if this command works on your system:
>
> grep -rl PATH /tmp /var/tmp
>
> then the same command generated using an array expansion will also work.

Do you understand how grep distinguishes the search pattern from the search 
directories ?

I would prefer that my script does not impose any restriction on the search 
patterns allowed
by grep, because currently "$ptrn" is just a user-defined string.  I am not 
experienced enough
to know what modifications would be required.  My plan is to allow any search 
pattern format
that grep could use.  But this also means that the user has to pass it to my 
script as an
argument.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]