Bash - issue with grep conditional statement

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Why does this code work correctly, while the other version of the same condition doesn't?



if grep -q string file; then
echo found
else
echo not found
fi


This doesn't work:



if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi






share|improve this question


















  • 1




    Why have you changed the syntax on the two examples?, just a ! to your original example
    – Raman Sailopal
    Oct 20 '17 at 9:04










  • Do you know what -q does, or are you just blindly using it because you've seen others use it?
    – chepner
    Oct 21 '17 at 1:27














up vote
0
down vote

favorite












Why does this code work correctly, while the other version of the same condition doesn't?



if grep -q string file; then
echo found
else
echo not found
fi


This doesn't work:



if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi






share|improve this question


















  • 1




    Why have you changed the syntax on the two examples?, just a ! to your original example
    – Raman Sailopal
    Oct 20 '17 at 9:04










  • Do you know what -q does, or are you just blindly using it because you've seen others use it?
    – chepner
    Oct 21 '17 at 1:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Why does this code work correctly, while the other version of the same condition doesn't?



if grep -q string file; then
echo found
else
echo not found
fi


This doesn't work:



if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi






share|improve this question














Why does this code work correctly, while the other version of the same condition doesn't?



if grep -q string file; then
echo found
else
echo not found
fi


This doesn't work:



if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi








share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 9:27









Jeff Schaller

32.1k849109




32.1k849109










asked Oct 20 '17 at 8:56









asd

33




33







  • 1




    Why have you changed the syntax on the two examples?, just a ! to your original example
    – Raman Sailopal
    Oct 20 '17 at 9:04










  • Do you know what -q does, or are you just blindly using it because you've seen others use it?
    – chepner
    Oct 21 '17 at 1:27












  • 1




    Why have you changed the syntax on the two examples?, just a ! to your original example
    – Raman Sailopal
    Oct 20 '17 at 9:04










  • Do you know what -q does, or are you just blindly using it because you've seen others use it?
    – chepner
    Oct 21 '17 at 1:27







1




1




Why have you changed the syntax on the two examples?, just a ! to your original example
– Raman Sailopal
Oct 20 '17 at 9:04




Why have you changed the syntax on the two examples?, just a ! to your original example
– Raman Sailopal
Oct 20 '17 at 9:04












Do you know what -q does, or are you just blindly using it because you've seen others use it?
– chepner
Oct 21 '17 at 1:27




Do you know what -q does, or are you just blindly using it because you've seen others use it?
– chepner
Oct 21 '17 at 1:27










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










`grep -q string file`, in backticks (or inside $(...), which is preferable), will be replaced by the output of grep. This will be an empty string since -q is used.



To negate a test, just insert ! before it:



if ! grep -q pattern file; then
echo not found
else
echo found
fi


If you truly want to search for a string (rather than a regular expression), then you should use -F with grep as well.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399303%2fbash-issue-with-grep-conditional-statement%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    `grep -q string file`, in backticks (or inside $(...), which is preferable), will be replaced by the output of grep. This will be an empty string since -q is used.



    To negate a test, just insert ! before it:



    if ! grep -q pattern file; then
    echo not found
    else
    echo found
    fi


    If you truly want to search for a string (rather than a regular expression), then you should use -F with grep as well.






    share|improve this answer


























      up vote
      5
      down vote



      accepted










      `grep -q string file`, in backticks (or inside $(...), which is preferable), will be replaced by the output of grep. This will be an empty string since -q is used.



      To negate a test, just insert ! before it:



      if ! grep -q pattern file; then
      echo not found
      else
      echo found
      fi


      If you truly want to search for a string (rather than a regular expression), then you should use -F with grep as well.






      share|improve this answer
























        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        `grep -q string file`, in backticks (or inside $(...), which is preferable), will be replaced by the output of grep. This will be an empty string since -q is used.



        To negate a test, just insert ! before it:



        if ! grep -q pattern file; then
        echo not found
        else
        echo found
        fi


        If you truly want to search for a string (rather than a regular expression), then you should use -F with grep as well.






        share|improve this answer














        `grep -q string file`, in backticks (or inside $(...), which is preferable), will be replaced by the output of grep. This will be an empty string since -q is used.



        To negate a test, just insert ! before it:



        if ! grep -q pattern file; then
        echo not found
        else
        echo found
        fi


        If you truly want to search for a string (rather than a regular expression), then you should use -F with grep as well.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 20 '17 at 11:07

























        answered Oct 20 '17 at 9:19









        Kusalananda

        105k14209326




        105k14209326



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399303%2fbash-issue-with-grep-conditional-statement%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Christian Cage

            How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?