I would like to know how to pass a bash variable as a pattern to awk. I have read several Q/As which tend to answer the same question, how
bash variable
pattern
awk
Variables aren't expanded in regexp literals (/.../). You need to use the ~ operator and string literals.
/.../
~
awk -v pattern="$y" '$0 ~ "^"pattern {print $2}' "$x" > "freec_${x}_${y}_st.txt"
You also need to be careful with any pattern metacharacters/etc. in the pattern.