In shell scripts, you should not write $variable , since this will do the extension of the word by the value of the variable. In your case, this leads to four words.
Always use quotation marks around variables, for example:
grep -e "$string" file...
-e needed when a line can begin with a dash, and the quotation marks around the line save it as a single word.
By the way: when writing shell programs, the first line should be set -eu . This allows * e * rror to check and check for * u * ndefined variables that will be useful in your case. Read more in the Bash manual.
source share