• Uncategorized

About bash : How-to-check-if-a-file-contains-a-specific-string-using-Bash

Question Detail

I want to check if a file contains a specific string or not in bash. I used this script, but it doesn’t work:

 if [[ 'grep 'SomeString' $File' ]];then
   # Some Actions

What’s wrong in my code?

Question Answer

if grep -q SomeString "$File"; then
  Some Actions # SomeString was found

You don’t need [[ ]] here. Just run the command directly. Add -q option when you don’t need the string displayed when it was found.

The grep command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.

$ echo hello | grep hi ; echo $?
$ echo hello | grep he ; echo $?
$ echo hello | grep -q he ; echo $?

You can specify commands as an condition of if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.

$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi

As you can see you run here the programs directly. No additional [] or [[]].

In case if you want to check whether file does not contain a specific string, you can do it as follows.

if ! grep -q SomeString "$File"; then
  Some Actions # SomeString was not found

In addition to other answers, which told you how to do what you wanted, I try to explain what was wrong (which is what you wanted.

In Bash, if is to be followed with a command. If the exit code of this command is equal to 0, then the then part is executed, else the else part if any is executed.

You can do that with any command as explained in other answers: if /bin/true; then ...; fi

[[ is an internal bash command dedicated to some tests, like file existence, variable comparisons. Similarly [ is an external command (it is located typically in /usr/bin/[) that performs roughly the same tests but needs ] as a final argument, which is why ] must be padded with a space on the left, which is not the case with ]].

Here you needn’t [[ nor [.

Another thing is the way you quote things. In bash, there is only one case where pairs of quotes do nest, it is "$(command "argument")". But in 'grep 'SomeString' $File' you have only one word, because 'grep ' is a quoted unit, which is concatenated with SomeString and then again concatenated with ' $File'. The variable $File is not even replaced with its value because of the use of single quotes. The proper way to do that is grep 'SomeString' "$File".

Shortest (correct) version:

grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"

can be also written as

grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"

but you dont need to explicitly test it in this case, so the same with:

grep -q "something" file && echo "yes" || echo "no"

##To check for a particular  string in a file

cd PATH_TO_YOUR_DIRECTORY #Changing directory to your working directory
if grep -q STRING_YOU_ARE_CHECKING_FOR "$File"; ##note the space after the string you are searching for
echo "Hooray!!It's available"
echo "Oops!!Not available"

grep -q [PATTERN] [FILE] && echo $?

The exit status is 0 (true) if the pattern was found; otherwise blankstring.

In case you want to checkif the string matches the whole line and if it is a fixed string, You can do it this way

grep -Fxq [String] [filePath]


 searchString="Hello World"
 if grep -Fxq "$searchString" $file
            echo "String found in $file"
            echo "String not found in $file"

From the man file:

-F, --fixed-strings

          Interpret  PATTERN  as  a  list of fixed strings, separated by newlines, any of 

which is to be matched.
          (-F is specified by POSIX.)
-x, --line-regexp
          Select only those matches that exactly match the whole line.  (-x is specified by 

-q, --quiet, --silent
          Quiet; do not write anything to standard output.  Exit immediately with zero 

status  if  any  match  is
          found,  even  if  an error was detected.  Also see the -s or --no-messages 

option.  (-q is specified by

if grep -q [string] [filename]
    [whatever action]


if grep -q 'my cat is in a tree' /tmp/cat.txt
    mkdir cat

Try this:

if [[ $(grep "SomeString" $File) ]] ; then
   echo "Found"
   echo "Not Found"

I done this, seems to work fine

if grep $SearchTerm $FileToSearch; then
   echo "$SearchTerm found OK"
   echo "$SearchTerm not found"

grep -q "something" file
[[ !? -eq 0 ]] && echo "yes" || echo "no"

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.