    #!/bin/shfunction checkExit(){    if test $? != 0; then      echo Command $1 exited with abnormal status      exit 1;    else echo $?    fi}function echoThenRun () { # echo and then run the command  echo $1  $1  ret=$?  echo $ret  return $ret}file=test_fileecho > $fileechoThenRun test -f $filecheckExit $fileecho all right!The output of executing the script:$  ~/Downloads/test.sh test -f test_file01 # why 1 here??all right!
How to use $? and test to check function?
bash;shell;exit;error handling
There's a simpler way of what you're doing. If you use set -x, the script will automatically echo each line before it's executed.Also, as soon as you execute another command, $? is replaced with the exit code of that command.You'll have to back it up to a variable if you're going to be doing anything with it other than a quick test-and-forget. The [ is actually a program that has its own exit code.For example:set -x # make sure the command echosexecute some command...result=$?set +x # undo command echoingif [ $result -ne 0 ]; then    echo Your command exited with non-zero status $resultfi