The $? identifier has always been a bit quirky :-) It has worked that way ever since it was added, so it's not something I can change since it might break scripts. The $? identifier was replaced with $input() to overcome these issues. Alternatively you can enclose $?="" in $() to control the evaluation order.