How do I grep for strings with special characters like []?

grep, linux

Solution

You are right that `[` and `]` are special characters. Quote them with `\` or use `fgrep` instead. The latter is plain string search:

fgrep "\$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']" ...

You still need to quote `$` though because it is otherwise interpreted by `bash` and other shells.

Problem

I am trying to find all the files has text: `$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']`, I tried to use `grep -rl '$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']' .`, but later on found `[]` has special meaning as search pattern. So what is the right command for searching text `$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']`?

Original source