It wouldn't work with the syntax you have. You are not populating the array with the result of grep
. You are not handling the string passed over the pipe and populating an empty array at the received end of the pipe.
Perhaps you were intending to do
array=($(echo v1.33.4 | egrep -o '[0-9]{1,3}'))
Notice how the echo
of the string is passed over to the standard input of egrep
which was missing in your attempt.
But as in the linked answer, using mapfile
would be the best option here because with the above approach if the search results contains words containing spaces, they would be stored in separated indices in the array rather than in a single one.
mapfile -t array < <(echo "v1.33.4" | egrep -o '[0-9]{1,3}')
printf '%s\n' "${array[@]}"
Notice the array expansion in bash
takes the syntax of "${array[@]}"
and not just a simple "${array}"
expansion.