Search code examples
bashexit-code

equivalent of exit on command line


I have a bash script that runs a series of python scripts. It always runs all of them, but exits with a failure code if any script exited with a failure code. At least that's what I hope it does. Here it is ...

#!/bin/bash

res=0

for f in scripts/*.py
do
  python "$f";
  res=$(( $res | $? ))
done

exit $res

I'd like to run this as a bash command in the terminal, but i can't work out how to replace exit so that the command fails like a failed script, rather than exits the terminal. How do I do that?


Solution

  • Replace your last line exit $res with

    $(exit ${res})
    

    This exits the spawned subshell with the exit value of ${res} and because it is the last statement, this is also the exit value of your script.