Bash Shell Script Needs a Variable Passed with Characters "Escaped"

I think @ccstone's solution is the best and simplest in this particular case. In more general situations you might try this:

grep -E "$(printf '%q' "$KMVAR_oo_line4")" ./Existing_Book_Split_Files.txt

The two key elements here are the use of grep -E which has superseded the deprecated egrep; and the printf command which has %q token that will escape basically every character that isn't a letter or number (the exact details are likely found on man printf, but essentially, it'll do what you need in a bash-shell context).

3 Likes