Question: I'm trying to get the exit code of the function that I'm repeatedly calling in the "condition" part of a Bash `while` loop:

```bash
while <function>; do 
    <stuff> 
done 

```

When this loop terminates due to an error, I need the exit code of `<function>`. Any thoughts on how I can get that?

Answer: 
Create a wrapping function that sets a variable with the result of the exit code of your function. Example:

```bsh
e=0

# file does not exist
foo() { 
  rm xyz
}

baz() { 
  foo
  e=$?
  [[ $e -ne 0 ]] && return 1
  return 0
}

# create a file
touch xyz
while baz; do
  # first loop succeeds, the file exists
  # second loop exits with error, file doesn't exists
  echo in loop
done

```

```none
in loop
rm: cannot remove 'xyz': No such file or directory

```

```
# then
$ echo $e
1

```
With `zsh`, you could use `return` from an anonymous function:

```
() while ((1)) {
  <function> || return
  <stuff>
}
print $?

```

With any Bourne-like shell, you can always use a named function:

```
repeat_until_fail()
  while true; do
    <function> || return
    <stuff>
  done

repeat_until_fail
echo "$?"

```