$? refers to the exit code of the last command run. The variable assignation is not a command, from a shell point of view. So this works (I just tested it :) )