Artificial intelligent assistant

Why is the exit status always 0 at the start of a script? I have a script like this, named `judge`: #!/bin/bash echo "last exit status is $?" It always outputs "last exit status is 0". Eg: ls -l; judge # correctly reports 0 ls -z; judge # incorrectly reports 0 beedogs; judge # incorrectly reports 0 Why?

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 judge
judge is a function
judge ()
{
echo "last exit status is $?"
}
[root@xxx httpd]# ls -l / >/dev/null 2>&1; judge
last exit status is 0
[root@xxx httpd]# ls -l /doesntExist >/dev/null 2>&1; judge
last exit status is 2
[root@xxx httpd]#

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 735eb521220f21ba535369682f4f28d6