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

I am trying to find all files with text: $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] , I tried using grep -rl '$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']' . , but later found [] is of particular importance as a search pattern. So what is the right command to search for text $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] ?

+6
source share
3 answers

You are right that [ and ] are special characters. Write them with \ or use fgrep . The latter is a simple string search:

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

You still need to quote $ , though, since that is otherwise interpreted by bash and other shells.

+13
source

You can use -F or -fixed-strings to specify a list of strings to be matched.

grep -Frl "\$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']" /path/to/files

On the man page for grep:

-F, --fixed-strings Interpret PATTERN as a list of fixed lines separated by newlines, any of which must be matched. (-F is specified by POSIX.)

fgrep deprecated, but may be available as an alternative to grep -F .

+2
source

As Maxim Egorushkin said, you need to avoid the characters $ and [].

 grep -rl "\$GLOBALS\['TYPO3_CONF_VARS'\]\['SC_OPTIONS'\]" . 
+1
source

Source: https://habr.com/ru/post/951696/


All Articles