What is the difference between using "-ne" and "!=" for evaluating the exit code in KornShell (ksh)?

StackOverflow https://stackoverflow.com/questions/20978669

  •  25-09-2022
  •  | 
  •  

Question

What is the difference between the following two KornShell (ksh) snippets because they both behave exactly the same thus far during testing? The return code (e.g., exit code, return status, exit status, returnCode) is from a SQL*Plus command if that matters.

kornShellSnippet1.ksh

returnCode=${?}
    if [[ ${returnCode} -ne 0 ]]; then #successful command returns 0#

kornShellSnippet2.ksh

returnCode=${?}
    if [[ ${returnCode} != 0 ]]; then #successful command returns 0#
Was it helpful?

Solution

-ne is a numeric test, != is a string test. Since you know $? is a number, it makes sense to use the numeric test.

OTHER TIPS

As far as I know -ne is legacy syntax supported for backward compatibility, while (within ksh double square brackets at least) != etc are the native ksh syntax.

For comparing numbers though, wouldn't you use the arithmetic syntax?

let returnCode=${?}

if (( returnCode != 0 )); then #successful command returns 0#
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top