Suppose I have a script like this:command1 paramscommand2 paramscommand3 paramsNow, as I understand, each of those commands will run independently of the return status of the previous one. But I want them all to run as a single transaction, i.e. if any previous command fails the script:should not proceedideally revert actions done by previous commandsFor the first question, I understand, that I can use && operator and chain all commands with it, or put if checks for $? after each command, but are these approaches the only options?And what is the standard approach for the second question?
How to ensure script is run successfully as a transaction
shell script
An alternative to chaining the commands together with && would be to run the script, or the section of the script, or sub-shell, with the -e option set (set -e, sometimes called the errexit option).  This would cause the script or sub-shell to simply exit on an error:( set -e  command1  command2  command3 )As for a rollback of already executed steps, this is probably impossible to achieve in a shell script.Take, for example, a script that performs an exit or a system reboot, or even manipulates data on disk:( set -e  if (( $# == 0 )); then    echo No command line arguments! >&2    exit  fi  echo We're cool  false )( set -e  case $cmd in    restart)  sudo reboot ;;    greet)    echo Hi there ;;    *)        echo Huh? ;;  esac  false )( set -e  rm -f /tmp/important_tmp_file  echo oh oh  false )There would not be any way of rolling back an exit from having been executed, or a reboot from having been performed, or to undelete a file automatically.  Notice, too, that output to the terminal is also out of the hands of the shell and can't be called back.