So, I have a .js file as below and I am trying to search with the below grep pattern. I am not sure what mistake I did but it does not give any output even though the file has that pattern. What mistake am I doing and how to correct it?

.js file:

var View = require('ampersand-view');
window.jQuery = require('jquery');
var extend = require('lodash/assign');

my requirement is to find any occurrences of: require('query')

grep I am using: grep 'require('jquery')' index.js


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');


To understand better what is happening, you can use echo statements to see how the shell processes strings:

$ echo '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')"

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'\'')'

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.

  • Depends on the shell, single quotes can be embedded in single-quoted strings in fish ('foo\'bar') and rc/es/akanga ('foo''bar') – Stéphane Chazelas Sep 26 '16 at 20:59
  • @StéphaneChazelas Interesting. I updated my answer to note the portability issues. – John1024 Sep 26 '16 at 22:45

Single quotes can not be nested, use double quotes around the pattern:

grep "require('jquery')" file.js

When you do:

grep 'require('jquery')' file.js

The shell is first breaking the pattern into 3 parts, based on the single quotes. First Literal 'require(', then jquery, and then literal ')'. So in effect the pattern is taken as require(jquery), which is not matching anything expectedly.


$ cat foo.js 
var View = require('ampersand-view');
window.jQuery = require('jquery');
var extend = require('lodash/assign');

$ grep "require('jquery')" foo.js
window.jQuery = require('jquery');

In most shells, the ' character is a quoting operator and the space character is used to separate arguments to a command. So your shell will parse that command as 3 arguments to pass to the /bin/grep command:

  1. grep
  2. require(jquery)
  3. index.js

In 2 above, the 'require('jquery')' has been taken as a quoted require( concatenated with an unquoted jquery concatenated with a quoted ).

You want 2 to be require('jquery'). Since ', like ( and ) is special character to the shell, you need to quote it in some way. The syntax depends on the shell. In Bourne-like shells, fish and (t)csh, you can use double-quote which they recognise as another quoting operator:

 grep "require('jquery')"

In rc-like shells (rc, akanga, es) where '...' is the only form of quoting, the syntax is:

 grep 'require(''jquery'')'

That double-' within '...' is the rc way to escape a ' there.

For details on the special characters in the various Unix shells and how to quote/escape them, see How to use a special character as a normal one?.


Put the search pattern in double quotes.

grep "require('jquery')" index.js
  • @ikkachu, Option 1 works in the fish shell. The OP didn't specify which shell he was using. – Stéphane Chazelas Sep 26 '16 at 20:55
  • I removed option 1 since I am not sure how many shells support it besides fish. – Peschke Sep 26 '16 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.