1. Grepping into 1 variable
foo1=$(curl https://domain.com/file.xml | grep -E "string1|string2")
This will run
curl 1 time and grep for occurrences of
2. Grepping into 2 variables
If they're different variables then change tactics slightly. Capture the output of
curl and then
foo1=$(echo "$output" | grep "string1")
foo2=$(echo "$output" | grep "string2")
3. Grepping into an array
You could also store the results in an array instead of separate variables.
readarray foo < <(echo "$output" | grep "string1|string2")
This one is a little tricky to deal with if your results from the grep might not return results, since the results from "string2" might be the first or second item in the array, but I'm providing it here just as a demonstration of the approach.
4. Reading into vars from grep
Yet another method which makes use of the
read command along with process substitution (
$ read -d"\n" foo1 foo2 \
<(curl https://domain.com/file.xml | grep -E "string1|string2")
This again can be tricky if the search for "string1" returns nothing, causing any matches for "string2" to show up in
$foo1. Also this approach tends to be less portable than either #2 or #3 above.