This works fine for me:
function foo() {
bar[0]="hello"
bar[1]="world"
return 1
}
if ! foo; then
echo "${bar[@]}"
fi
https://onlinegdb.com/xPIFP110w
Are you getting messed up by the way bash handles exit statuses? An exit status of 0 indicates a success and a non-zero exit status indicates a failure (which allows for the different exit statuses to indicate different errors).
So if my_func; then something; fi
will only run something
when my_func
returns 0. In your case, you’re using !
to do the opposite so it only runs when your function returns a non-zero status.
This can be quite surprising if you’re expecting the behavior found in other languages like python or C++ where 0 represents false and 1 represents true.
Stupid people require oxygen to live.