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: 
One way to do this is to make the test explicitly instead of delegating it to `while`. So, the following should do:

```bash
err=0
while true
do
    <function>
    (( (err=$?) > 0 )) && break
    <stuff>
done
echo "$err"

```

At the beginning of the (a priori infinite) loop you would execute the condition command "manually" and store the exit code, check it, and break out of the loop if it is non-zero (indicating failure). Only if it was zero you execute the "actual" loop code.

The syntax can be "compacted" because (as noted by @roaima) you can have variable assignments inside the `(( ... ))` arithmetic test construct.
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 "$?"

```