I set DID to 1 so to see your condition work... in other words your script will run with DID=0 always since the command producing the result of it ($?) at that point is successful...
I know Im not very clear but its friday...
So your script now :
for NUM in 1 2 3
do
echo $NUM
for LET in A B C
do
echo $LET
let DID=$?
echo DID: $DID
[ $DID != 0 ] && echo "echo failed"
echo $?
done
done
There is nothing wrong. What you are seeing is correct behavior. In each case you are seeing the exit status of the last command to run.
In the first case, the last command before echoing $? is [ $? != 0 ] . The test failed and so $? is set to 1.
In the second case, you're seeing the exit status of the if-statement itself. Since the conditional test fails, the if-statement does not execute further, the list of commands after then does not execute. So the if-statement itself sets $? to 0.