简体   繁体   中英

What does -ex option used in bash | #!/bin/bash -ex mean

We are using the script below as user data for EC2 instances. I don't understand what is -ex option is being used for?

#!/bin/bash -ex

yum update -y
yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
service httpd start
chkconfig httpd on

According to Add tack ex on your bash shebang | #!/bin/bash -ex

Bash scripts can use various options on the shebang (#!/bin/bash). A more common one is: '#!/bin/bash -ex'.

-e Exit immediately if a command exits with a non-zero status.
-x Print commands and their arguments as they are executed.

In short, adding -ex to your #!/bin/bash will give verbose output and also will abort your script immediately if part of the script fails.

This is just another way of saying :

#!/bin/bash 

set -ex # Added line

yum update -y
yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
service httpd start
chkconfig httpd on

[ bash manual ] says :

All of the single-character options used with the set builtin (see The Set Builtin) can be used as options when the shell is invoked

[ set reference ] says :

set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables.

-x

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

-e

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists), or a compound command (see Compound Commands) returns a non-zero status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM