1

I have looked at the bash function return values and it seems I still don't understand how that works. I setup 3 function each supposedly returning a RET_VAL return value. I assume func_1 returns either a 1 or 0 based on the if statement I assume func_2 return either a 1 or a 0 based on the if statement I assume func_3 should either print a 1 or a 0 based on the return value of func_2

func_1() {
  RET_VAL=0
  if [ -d /tmp/dir ]; then
    echo "Dir exists" 
    RET_VAL=0
  else
    echo "Dir doesn't exist"
    RET_VAL=1
  fi
  return ${RET_VAL}
}

func_2() {
  RET_VAL=0
  if func_1; then
    if [ -f /tmp/file_1]; then
      echo "File exists"
    else
      echo "File doesn't exist"
      RET_VAL=1
    fi
  else
    RET_VAL=1
  fi
  return ${RET_VAL}
}

func_3() {
  if func_2; then
    echo "Dir and File do exist"
  else
    echo "Dir and file do not exist"
  fi
}

Are my assumptions correct or is each function returning what it executed last, like the echo statement? If so, how could I assure that the functions return a 1 or a 0 value?

Cheers, Roland

1
  • Beware that shell variables are not function scoped by default, so the assignment of RET_VAL at the top of func_2 will be immediately overwritten by the ones inside func_1. (In this case this happens to have no net effect, in the behavior of the code, though). Commented Apr 28, 2019 at 23:14

1 Answer 1

2

func_3 will not print the return value of func_2. The if statement will use the exit status of func_2 to determine which branch to take. Every command has an exit status, so in the absence of an explicit return command, the exit status of the last command to execute will be the exit status of a function. In the case of func_3, the exit status will be the exit status of whichever echo command executes (which is virtually always 0, absent any I/O errors).

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you @chepner for your fast response. So, if I understand correctly, if I would replace RET_VAL in each statement with an actual return 0, or return 1, this will return the actual 0 or 1 value and not the echo statement? Thank you
There's a difference between the exit status ("return value") of a function and its output. echo produces output; return sets the exit status of the function and immediately returns to the caller. Your assumptions about func_1 and func_2 appear to be correct.
Yes I do understand that echo and return have different behavior, function returns. Yet what confuses me is the last function statement is always a "return/exit" status set to either 0 or 1, and so why would the calling function not get the expected return value, or exit status, of 0 or 1?
echo just writes to standard output. Stop thinking of what it does as the return value of a function; you can have as many echo statements as you like in a function, and none of them cause the function to exit immediately. echo and return are not interchangeable. return can only return an integer value between 0 and 255.
$(...) captures the standard output of a command; it has nothing to do with capturing the exit status. The exit status of the most recent command is always found in $?.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.