Using a variable in a regex
You've made a few mistakes there. Here's your script corrected:
while read line
do
if [[ "$line" =~ $key ]]; then
echo $line
fi
done < "$filename"
Though it seems it could be replaced with a simple grep
:
grep "$key" "$filename"