Bash: How to escape minus in `if [ … ` (alias “test”)?

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











up vote
-1
down vote

favorite












I need to test this:



kill -0 $LAST_PID;


So I wrote



if [ kill -0 $LAST_PID ] ; then ...


But this results in an error - unknown operator after - or so.



how do I escape the options within if?










share|improve this question



















  • 1




    Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
    – Ondra Žižka
    Aug 15 at 23:44














up vote
-1
down vote

favorite












I need to test this:



kill -0 $LAST_PID;


So I wrote



if [ kill -0 $LAST_PID ] ; then ...


But this results in an error - unknown operator after - or so.



how do I escape the options within if?










share|improve this question



















  • 1




    Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
    – Ondra Žižka
    Aug 15 at 23:44












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I need to test this:



kill -0 $LAST_PID;


So I wrote



if [ kill -0 $LAST_PID ] ; then ...


But this results in an error - unknown operator after - or so.



how do I escape the options within if?










share|improve this question















I need to test this:



kill -0 $LAST_PID;


So I wrote



if [ kill -0 $LAST_PID ] ; then ...


But this results in an error - unknown operator after - or so.



how do I escape the options within if?







scripting kill test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 15 at 16:27









Jeff Schaller

32.6k849110




32.6k849110










asked Aug 15 at 16:23









Ondra Žižka

467311




467311







  • 1




    Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
    – Ondra Žižka
    Aug 15 at 23:44












  • 1




    Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
    – Ondra Žižka
    Aug 15 at 23:44







1




1




Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
– Ondra Žižka
Aug 15 at 23:44




Not sure why it's downvoted but since it is, it's probably a bad question and needs to be deleted.
– Ondra Žižka
Aug 15 at 23:44










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










It's not if that you're trying to escape it from, it's test / [. But you aren't testing the executing of kill that way; you're testing strings. You want:



if kill -0 "$LAST_PID"; then ...


that will test kill's return code.






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%2f462778%2fbash-how-to-escape-minus-in-if-alias-test%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
    4
    down vote



    accepted










    It's not if that you're trying to escape it from, it's test / [. But you aren't testing the executing of kill that way; you're testing strings. You want:



    if kill -0 "$LAST_PID"; then ...


    that will test kill's return code.






    share|improve this answer
























      up vote
      4
      down vote



      accepted










      It's not if that you're trying to escape it from, it's test / [. But you aren't testing the executing of kill that way; you're testing strings. You want:



      if kill -0 "$LAST_PID"; then ...


      that will test kill's return code.






      share|improve this answer






















        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        It's not if that you're trying to escape it from, it's test / [. But you aren't testing the executing of kill that way; you're testing strings. You want:



        if kill -0 "$LAST_PID"; then ...


        that will test kill's return code.






        share|improve this answer












        It's not if that you're trying to escape it from, it's test / [. But you aren't testing the executing of kill that way; you're testing strings. You want:



        if kill -0 "$LAST_PID"; then ...


        that will test kill's return code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 15 at 16:27









        Jeff Schaller

        32.6k849110




        32.6k849110



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462778%2fbash-how-to-escape-minus-in-if-alias-test%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay