79081961

Date: 2024-10-12 21:27:37
Score: 1
Natty:
Report link

The solution of jfgiraud can be slightly modified when you have problem with the mapfile:
To set an array variable from any source that generates one file name per line use:
IFS=$'\n' avar=( $(ls -1) )
or
IFS=$'\n' avar =( $( < listOfFiles.txt) )
etc.
Then use the variable $avar in argument list as "${avar[@]}"
To see the proper argument parsing try: printf "%s\n" "${avar[@]}"
Particularly, for the given question it would be read as :
IFS=$'\n' files =( $( < listOfFiles.txt) ) ; grep 'regex' -- "${files[@]}"
Note:

  1. If the count of elements in $avar exceeds the argument count system limit use repeated bash "Substring Expansion" e.g. "${avar[@]:offset:length}"
  2. The last grep option -- is useful when the file list may contain a names with hyphen as the first letter.
  3. Windows line ending '\r\n' may be correctly processed by eliminating '\r' right in the generator, e.g.
    $( tr -d '\r' <listOfFiles.txt )
Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: kazo