Single quotes cannot be embedded in single-quoted strings. Try:
$ grep "require('jquery')" index.js
window.jQuery = require('jquery');
Alternatively, you can end the single-quoted string, add an escaped single-quote, and then restart the single-quoted string:
$ grep 'require('\''jquery'\'')' index.js
window.jQuery = require('jquery');
Discussion
To understand better what is happening, you can use echo statements to see how the shell processes strings:
$ echo 'require('jquery')'
require(jquery)
In the above example, there are two single-quoted strings: require(
and )
. As far as the shell is concerned, the string jquery
is unquoted.
$ echo "require('jquery')"
require('jquery')
Because the shell accepts single-quotes as part of double-quoted strings, the above works fine.
Sometimes, to avoid shell expansions, one needs everything to be in a single-quoted string. In that case:
$ echo 'require('\''jquery'\'')'
require('jquery')
In the above, there are three single-quoted strings: require(
, jquery
, and )
. In between those strings are single-quotes that are escaped so that the shell treats them as normal characters.
This answer assumes that the shell is bash, dash, ash, or other POSIX or bourne-derived shell. For information on still other shells, see Stéphane Chazelas' answer.