Word-Splitting is occurring on your GREP_OPTS="-e \"test this\""
resulting in the command being
grep -e '"test' 'this"'
Resulting in the exact error:
grep: this": No such file or directory
(of course there is no file named "this\""
)
See BashFAQ-50 - I'm trying to put a command in a variable, but the complex cases always fail.
In order to prevent word splitting use an array for options instead of trying to use a single variable, e.g.
#!/bin/sh
set -xe
GREP_OPTS=(-e "test this")
echo "I want to test this." | grep "${GREP_OPTS[@]}"
Example Use/Output
$ bash grepopts.sh
+ GREP_OPTS=(-e "test this")
+ echo 'I want to test this.'
+ grep -e 'test this'
I want to test this.
Let me know if you have further questions.