Exit statuses of comparisons in test constructs2019 Community Moderator ElectionPOSIX test and -adifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test commandDifference in conditions /test( test -n $st ) != ( test -z $st ) right?How to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?

Delete multiple columns using awk or sed

Has any country ever had 2 former presidents in jail simultaneously?

Is there a RAID 0 Equivalent for RAM?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Multiplicative persistence

How could a planet have erratic days?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Why Shazam when there is already Superman?

It grows, but water kills it

How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Were Persian-Median kings illiterate?

Can I turn my anal-retentiveness into a career?

How can I write humor as character trait?

Change the color of a single dot in `ddot` symbol

Taxes on Dividends in a Roth IRA

Which Article Helped Get Rid of Technobabble in RPGs?

What is Cash Advance APR?

Pre-mixing cryogenic fuels and using only one fuel tank

Shouldn’t conservatives embrace universal basic income?

How to preserve electronics (computers, iPads and phones) for hundreds of years

How many arrows is an archer expected to fire by the end of the Tyranny of Dragons pair of adventures?

Non-trope happy ending?

Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?



Exit statuses of comparisons in test constructs



2019 Community Moderator ElectionPOSIX test and -adifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test commandDifference in conditions /test( test -n $st ) != ( test -z $st ) right?How to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?










3















I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my findings in the following code snippet.



As you can see



rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"


Outputs



es_num is 1
es_str is 0


Is there any documentation, preferably from the POSIX standards, that talks about the difference in the exit statuses of -eq and = in a test construct?



What should I be aware of when writing conditional statements? What are some best practices regarding this?



Portable code is preferable to Bash code (which I'm using).










share|improve this question
























  • I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

    – Elegance
    Mar 7 at 19:16







  • 1





    The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

    – Kusalananda
    Mar 7 at 19:25











  • @ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

    – Elegance
    Mar 7 at 21:29












  • @Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

    – ilkkachu
    Mar 7 at 21:52












  • @ilkkachu Thank you for the helpful feedback.

    – Elegance
    Mar 7 at 22:08















3















I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my findings in the following code snippet.



As you can see



rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"


Outputs



es_num is 1
es_str is 0


Is there any documentation, preferably from the POSIX standards, that talks about the difference in the exit statuses of -eq and = in a test construct?



What should I be aware of when writing conditional statements? What are some best practices regarding this?



Portable code is preferable to Bash code (which I'm using).










share|improve this question
























  • I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

    – Elegance
    Mar 7 at 19:16







  • 1





    The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

    – Kusalananda
    Mar 7 at 19:25











  • @ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

    – Elegance
    Mar 7 at 21:29












  • @Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

    – ilkkachu
    Mar 7 at 21:52












  • @ilkkachu Thank you for the helpful feedback.

    – Elegance
    Mar 7 at 22:08













3












3








3








I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my findings in the following code snippet.



As you can see



rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"


Outputs



es_num is 1
es_str is 0


Is there any documentation, preferably from the POSIX standards, that talks about the difference in the exit statuses of -eq and = in a test construct?



What should I be aware of when writing conditional statements? What are some best practices regarding this?



Portable code is preferable to Bash code (which I'm using).










share|improve this question
















I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my findings in the following code snippet.



As you can see



rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"


Outputs



es_num is 1
es_str is 0


Is there any documentation, preferably from the POSIX standards, that talks about the difference in the exit statuses of -eq and = in a test construct?



What should I be aware of when writing conditional statements? What are some best practices regarding this?



Portable code is preferable to Bash code (which I'm using).







test control-flow






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 21:28







Elegance

















asked Mar 7 at 18:35









EleganceElegance

183




183












  • I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

    – Elegance
    Mar 7 at 19:16







  • 1





    The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

    – Kusalananda
    Mar 7 at 19:25











  • @ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

    – Elegance
    Mar 7 at 21:29












  • @Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

    – ilkkachu
    Mar 7 at 21:52












  • @ilkkachu Thank you for the helpful feedback.

    – Elegance
    Mar 7 at 22:08

















  • I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

    – Elegance
    Mar 7 at 19:16







  • 1





    The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

    – Kusalananda
    Mar 7 at 19:25











  • @ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

    – Elegance
    Mar 7 at 21:29












  • @Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

    – ilkkachu
    Mar 7 at 21:52












  • @ilkkachu Thank you for the helpful feedback.

    – Elegance
    Mar 7 at 22:08
















I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

– Elegance
Mar 7 at 19:16






I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu

– Elegance
Mar 7 at 19:16





1




1





The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

– Kusalananda
Mar 7 at 19:25





The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash).

– Kusalananda
Mar 7 at 19:25













@ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

– Elegance
Mar 7 at 21:29






@ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question.

– Elegance
Mar 7 at 21:29














@Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

– ilkkachu
Mar 7 at 21:52






@Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...)

– ilkkachu
Mar 7 at 21:52














@ilkkachu Thank you for the helpful feedback.

– Elegance
Mar 7 at 22:08





@ilkkachu Thank you for the helpful feedback.

– Elegance
Mar 7 at 22:08










2 Answers
2






active

oldest

votes


















3














-eq




True if the integers n1 and n2 are algebraically equal; otherwise, false.




test



=




True if the strings s1 and s2 are identical; otherwise, false.




test



So -eq compares integers and = compares strings (which will also work with some limited integer cases).




You do have a syntax issue though, it should be:



[ "$rc" = 0 ]


And not



[ $rc=0 ]


[ "$rc" = 0 ] should exit with 1 because rc does not equal 0



[ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true




With the sh [ test there are a few differences:



# leading 0
$ [ 01 -eq 1 ]; echo $?
0
# adjacent whitespace
$ [ ' 1' -eq 1 ]; echo $?
0
# negative 0 vs positive 0
$ [ 0 -eq -0 ]; echo $?
0


However with the bash [[ test there are a large number of differences (Including the ones mentioned above):



# base 8
$ [[ 032 -eq 26 ]]; echo $?
0
# Arithmetic expressions
$ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
0
# Base 64
$ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
0





share|improve this answer

























  • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

    – Elegance
    Mar 7 at 19:17












  • Any examples with strings that have nothing but digits, and don't have leading zeros?

    – Elegance
    Mar 7 at 19:21






  • 1





    @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

    – Kusalananda
    Mar 7 at 19:43







  • 1





    @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

    – Kusalananda
    Mar 7 at 20:02












  • @Jesse_b You shared the same link twice. I take it the first one is incorrect.

    – Elegance
    Mar 7 at 22:09


















0














For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).



One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If






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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f504979%2fexit-statuses-of-comparisons-in-test-constructs%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    -eq




    True if the integers n1 and n2 are algebraically equal; otherwise, false.




    test



    =




    True if the strings s1 and s2 are identical; otherwise, false.




    test



    So -eq compares integers and = compares strings (which will also work with some limited integer cases).




    You do have a syntax issue though, it should be:



    [ "$rc" = 0 ]


    And not



    [ $rc=0 ]


    [ "$rc" = 0 ] should exit with 1 because rc does not equal 0



    [ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true




    With the sh [ test there are a few differences:



    # leading 0
    $ [ 01 -eq 1 ]; echo $?
    0
    # adjacent whitespace
    $ [ ' 1' -eq 1 ]; echo $?
    0
    # negative 0 vs positive 0
    $ [ 0 -eq -0 ]; echo $?
    0


    However with the bash [[ test there are a large number of differences (Including the ones mentioned above):



    # base 8
    $ [[ 032 -eq 26 ]]; echo $?
    0
    # Arithmetic expressions
    $ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
    0
    # Base 64
    $ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
    0





    share|improve this answer

























    • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

      – Elegance
      Mar 7 at 19:17












    • Any examples with strings that have nothing but digits, and don't have leading zeros?

      – Elegance
      Mar 7 at 19:21






    • 1





      @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

      – Kusalananda
      Mar 7 at 19:43







    • 1





      @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

      – Kusalananda
      Mar 7 at 20:02












    • @Jesse_b You shared the same link twice. I take it the first one is incorrect.

      – Elegance
      Mar 7 at 22:09















    3














    -eq




    True if the integers n1 and n2 are algebraically equal; otherwise, false.




    test



    =




    True if the strings s1 and s2 are identical; otherwise, false.




    test



    So -eq compares integers and = compares strings (which will also work with some limited integer cases).




    You do have a syntax issue though, it should be:



    [ "$rc" = 0 ]


    And not



    [ $rc=0 ]


    [ "$rc" = 0 ] should exit with 1 because rc does not equal 0



    [ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true




    With the sh [ test there are a few differences:



    # leading 0
    $ [ 01 -eq 1 ]; echo $?
    0
    # adjacent whitespace
    $ [ ' 1' -eq 1 ]; echo $?
    0
    # negative 0 vs positive 0
    $ [ 0 -eq -0 ]; echo $?
    0


    However with the bash [[ test there are a large number of differences (Including the ones mentioned above):



    # base 8
    $ [[ 032 -eq 26 ]]; echo $?
    0
    # Arithmetic expressions
    $ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
    0
    # Base 64
    $ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
    0





    share|improve this answer

























    • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

      – Elegance
      Mar 7 at 19:17












    • Any examples with strings that have nothing but digits, and don't have leading zeros?

      – Elegance
      Mar 7 at 19:21






    • 1





      @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

      – Kusalananda
      Mar 7 at 19:43







    • 1





      @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

      – Kusalananda
      Mar 7 at 20:02












    • @Jesse_b You shared the same link twice. I take it the first one is incorrect.

      – Elegance
      Mar 7 at 22:09













    3












    3








    3







    -eq




    True if the integers n1 and n2 are algebraically equal; otherwise, false.




    test



    =




    True if the strings s1 and s2 are identical; otherwise, false.




    test



    So -eq compares integers and = compares strings (which will also work with some limited integer cases).




    You do have a syntax issue though, it should be:



    [ "$rc" = 0 ]


    And not



    [ $rc=0 ]


    [ "$rc" = 0 ] should exit with 1 because rc does not equal 0



    [ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true




    With the sh [ test there are a few differences:



    # leading 0
    $ [ 01 -eq 1 ]; echo $?
    0
    # adjacent whitespace
    $ [ ' 1' -eq 1 ]; echo $?
    0
    # negative 0 vs positive 0
    $ [ 0 -eq -0 ]; echo $?
    0


    However with the bash [[ test there are a large number of differences (Including the ones mentioned above):



    # base 8
    $ [[ 032 -eq 26 ]]; echo $?
    0
    # Arithmetic expressions
    $ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
    0
    # Base 64
    $ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
    0





    share|improve this answer















    -eq




    True if the integers n1 and n2 are algebraically equal; otherwise, false.




    test



    =




    True if the strings s1 and s2 are identical; otherwise, false.




    test



    So -eq compares integers and = compares strings (which will also work with some limited integer cases).




    You do have a syntax issue though, it should be:



    [ "$rc" = 0 ]


    And not



    [ $rc=0 ]


    [ "$rc" = 0 ] should exit with 1 because rc does not equal 0



    [ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true




    With the sh [ test there are a few differences:



    # leading 0
    $ [ 01 -eq 1 ]; echo $?
    0
    # adjacent whitespace
    $ [ ' 1' -eq 1 ]; echo $?
    0
    # negative 0 vs positive 0
    $ [ 0 -eq -0 ]; echo $?
    0


    However with the bash [[ test there are a large number of differences (Including the ones mentioned above):



    # base 8
    $ [[ 032 -eq 26 ]]; echo $?
    0
    # Arithmetic expressions
    $ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
    0
    # Base 64
    $ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
    0






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 20:32

























    answered Mar 7 at 18:50









    Jesse_bJesse_b

    13.8k23471




    13.8k23471












    • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

      – Elegance
      Mar 7 at 19:17












    • Any examples with strings that have nothing but digits, and don't have leading zeros?

      – Elegance
      Mar 7 at 19:21






    • 1





      @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

      – Kusalananda
      Mar 7 at 19:43







    • 1





      @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

      – Kusalananda
      Mar 7 at 20:02












    • @Jesse_b You shared the same link twice. I take it the first one is incorrect.

      – Elegance
      Mar 7 at 22:09

















    • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

      – Elegance
      Mar 7 at 19:17












    • Any examples with strings that have nothing but digits, and don't have leading zeros?

      – Elegance
      Mar 7 at 19:21






    • 1





      @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

      – Kusalananda
      Mar 7 at 19:43







    • 1





      @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

      – Kusalananda
      Mar 7 at 20:02












    • @Jesse_b You shared the same link twice. I take it the first one is incorrect.

      – Elegance
      Mar 7 at 22:09
















    I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

    – Elegance
    Mar 7 at 19:17






    I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison?

    – Elegance
    Mar 7 at 19:17














    Any examples with strings that have nothing but digits, and don't have leading zeros?

    – Elegance
    Mar 7 at 19:21





    Any examples with strings that have nothing but digits, and don't have leading zeros?

    – Elegance
    Mar 7 at 19:21




    1




    1





    @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

    – Kusalananda
    Mar 7 at 19:43






    @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison.

    – Kusalananda
    Mar 7 at 19:43





    1




    1





    @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

    – Kusalananda
    Mar 7 at 20:02






    @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true.

    – Kusalananda
    Mar 7 at 20:02














    @Jesse_b You shared the same link twice. I take it the first one is incorrect.

    – Elegance
    Mar 7 at 22:09





    @Jesse_b You shared the same link twice. I take it the first one is incorrect.

    – Elegance
    Mar 7 at 22:09













    0














    For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).



    One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If






    share|improve this answer



























      0














      For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).



      One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If






      share|improve this answer

























        0












        0








        0







        For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).



        One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If






        share|improve this answer













        For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).



        One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 18:55









        freiheitsnetzfreiheitsnetz

        112




        112



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504979%2fexit-statuses-of-comparisons-in-test-constructs%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived