Store grep output in an array
You can use:
targets=($(grep -HRl "pattern" .))
Note use of (...)
for array creation in BASH.
Also you can use grep -l
to get only file names in grep
's output (as shown in my command).
Above answer (written 7 years ago) made an assumption that output filenames won't contain special characters like whitespaces or globs. Here is a safe way to read those special filenames into an array: (will work with older bash versions)
while IFS= read -rd ''; do
targets+=("$REPLY")
done < <(grep --null -HRl "pattern" .)
# check content of array
declare -p targets
On BASH 4+ you can use readarray
instead of a loop:
readarray -d '' -t targets < <(grep --null -HRl "pattern" .)