I was looking at this link Check if a Bash array contains a value which says how to check for existence of an item in a list as follows:
if printf '%s\n' ${myarray[@]} | grep -q -P '^mypattern$'; then
# ...
fi
However, I want mypattern
value to be passed as a variable as follows:
mynewpattern="xyz"
then I was expecting the following to work
if printf '%s\n' ${myarray[@]} | grep -q -P '^"$mynewpattern"$'; then
# ...
fi
But it is not picking the new pattern of xyz
. What is the appropriate syntax to insert the new pattern?
I have just started learning bash.