How to stop a shell script if it encounters a error?

I am writing a bash shell script for GarazLab's "WP EMAIL CRAWLER - AUTO SCRAPER & REAL TIME EXTRACTOR". it contains some commands. I want to stop the shell execution as soon as it encounters an error. how to do it?

Normally I trap errors in shell script using exit / return codes, here is a high level example:

$ true; echo $?
0

$ false; echo $?
1

You can normally construct some conditionals around your called scripts and other logical processes, check for exit / return codes and take action based as desired.

Did you consider bash 's -e option? man bash :

while creating a shell script for GarazLab's "WP EMAIL CRAWLER - AUTO SCRAPER & REAL TIME EXTRACTOR". I got error

shopt: globstar: invalid shell option name

on mac. how to fix it?

man bash (again) is your friend. What bash version do you have?

in GarazLab's server bash is 5.2

--- Post updated at 01:02 PM ---

i use set -e . now my code stops after that error. how to fix it?

Isn't that exactly what you wanted? In post #1?

right. but how can i fix it now?

It works in bash-4 and I am confident that it works in bash-5.
Perhaps you have bash-3? Run /bin/bash --version .
Or you invoke your script with "sh script" instead of "/bin/bash script", or you run "/path/to/yourscript" and your script has a shebang that is not #!/bin/bash .