I have copied a shell script and it is pretty much simple but on the first line with shebang, the -xe
flag is specified.
#!/bin/bash -xe
Can anyone explain what does it do? I have searched but could not find any helping material.
CodePudding user response:
This is adding shell options to the shebang:
-x
,-o xtrace
: Trace display executed statements.-e
,-o errexit
: Exit on error, but inconsistently for legacy POSIX compliance.
#!/bin/bash -xe
This shebang setting combines two discouraged bad practices with their own set of issues:
- First bad practice is setting shell options in the shebang; because it means they will be ignored when invoking the script directly with:
bash script_name
, rather than making the script file executable, and invoking./script_name
.
Options shall be set explicitly in code withset -x
, or preferably use their more straightforward long name (example:set -o xtrace
instead ofset -x
). - Second bad practice is using the
-e
option orset -o errexit
which is only there for legacy POSIX compliance, but acts so inconsistently in different situations; that it causes more bugs than it avoids.
Here are some references about those set -e
, set -o errexit
quirks and issues: