Why is my bash function returning an unexpected return code?

Ryan V. Bissell

I have been creating a small library of bash functions to encapsulate some of the more arcane bash syntax structures into routines that I can quickly use and reference. But for some of them, I'm running into unexpected return codes from my functions. The 'is_undefined' function below is one such example. Can anyone explain the results I am getting? (Also provided below.)

#!/bin/bash

is_undefined ()
{
  # aka "unset" (not to be confused with "set to nothing")
  # http://stackoverflow.com/questions/874389/bash-test-for-a-variable-unset-using-a-function
  [ -z ${1+x} ]
}

if [ -z ${UNDEFINED+x} ]; then
  echo "inline method reports that \$UNDEFINED is undefined"
fi

if is_undefined UNDEFINED; then
  echo "is_undefined() reports that \$UNDEFINED is undefined"
else
  echo "is_undefined() reports that \$UNDEFINED is defined"
fi

DEFINED=
if is_undefined DEFINED; then
  echo "is_undefined() reports that \$DEFINED is undefined"
else
  echo "is_undefined() reports that \$DEFINED is defined"
fi

The surprising results are:

$ ./test.sh
inline method reports that $UNDEFINED is undefined
is_undefined() reports that $UNDEFINED is defined
is_undefined() reports that $DEFINED is defined
Jan Matějka

inside is_undefined you are testing $1, not ${UNDEFINED}, to do that you need throw in variable indirection like

is_undefined () {
    [ -z "${!1+x}" ]
}

However, that is bashism and not posix compliant. For posix compliacy you will need

is_undefined () {
    eval "[ -z \${$1+x} ]"
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my code returning a number lower than what it should return?

From Dev

Although my code is returning String the function still expects to return string

From Dev

Why is my function returning garbage when it should return a char?

From Dev

Why is my function returning garbage when it should return a char?

From Dev

Why the results of my code are unexpected?

From Dev

Why the results of my code are unexpected?

From Dev

Why is my code returning my else: statement?

From Dev

why this code return unexpected result? (conditional variable)

From Dev

why in this code would I be getting "Unexpected non-void return value in void function"

From Dev

Why this function return is not returning expected results in Java?

From Dev

Why this function return is not returning expected results in Java?

From Dev

Why my SQL Function returns unexpected value?

From Dev

Why my SQL Function returns unexpected value?

From Dev

Why is my "return" outside the function?

From Java

Why is my Jsoup Code not Returning the Correct Elements?

From Dev

why my code is returning 403 forbidden acess

From Dev

Why is my code returning it's results in quotes?

From Dev

Why is my code returning an incorrect value?

From Dev

Why is my jQuery widget returning 'object is not a function'?

From Dev

Why isn't my function returning?

From Dev

C - Why is my function returning NULL?

From Dev

Why is my javascript function not returning true?

From Dev

Why isn't my function returning the string?

From Dev

Why my function is returning empty string in python?

From Dev

Why is my function returning a Unit instead of Int?

From Dev

Why is my recursive JavaScript function not returning the string?

From Dev

Why is my function returning an array with undefined as items?

From Dev

Powershell - why is my function returning null?

From Dev

Why does my return statement ignore the rest of my code in a function in python?

Related Related

  1. 1

    Why is my code returning a number lower than what it should return?

  2. 2

    Although my code is returning String the function still expects to return string

  3. 3

    Why is my function returning garbage when it should return a char?

  4. 4

    Why is my function returning garbage when it should return a char?

  5. 5

    Why the results of my code are unexpected?

  6. 6

    Why the results of my code are unexpected?

  7. 7

    Why is my code returning my else: statement?

  8. 8

    why this code return unexpected result? (conditional variable)

  9. 9

    why in this code would I be getting "Unexpected non-void return value in void function"

  10. 10

    Why this function return is not returning expected results in Java?

  11. 11

    Why this function return is not returning expected results in Java?

  12. 12

    Why my SQL Function returns unexpected value?

  13. 13

    Why my SQL Function returns unexpected value?

  14. 14

    Why is my "return" outside the function?

  15. 15

    Why is my Jsoup Code not Returning the Correct Elements?

  16. 16

    why my code is returning 403 forbidden acess

  17. 17

    Why is my code returning it's results in quotes?

  18. 18

    Why is my code returning an incorrect value?

  19. 19

    Why is my jQuery widget returning 'object is not a function'?

  20. 20

    Why isn't my function returning?

  21. 21

    C - Why is my function returning NULL?

  22. 22

    Why is my javascript function not returning true?

  23. 23

    Why isn't my function returning the string?

  24. 24

    Why my function is returning empty string in python?

  25. 25

    Why is my function returning a Unit instead of Int?

  26. 26

    Why is my recursive JavaScript function not returning the string?

  27. 27

    Why is my function returning an array with undefined as items?

  28. 28

    Powershell - why is my function returning null?

  29. 29

    Why does my return statement ignore the rest of my code in a function in python?

HotTag

Archive