I have a script like this, named judge:#!/bin/bashecho last exit status is $?It always outputs last exit status is 0. Eg:ls -l;   judge # correctly reports 0ls -z;   judge # incorrectly reports 0beedogs; judge # incorrectly reports 0Why?
Why is the exit status always 0 at the start of a script?
exit
There are different bash processes executing each line of code and $? isn't shared between the processes. You can work around this by making judge a bash function:[root@xxx httpd]# type judgejudge is a functionjudge (){    echo last exit status is $?}[root@xxx httpd]# ls -l / >/dev/null 2>&1; judgelast exit status is 0[root@xxx httpd]# ls -l /doesntExist >/dev/null 2>&1; judgelast exit status is 2[root@xxx httpd]#