Home > Software engineering >  What does "-xe" with Shebang (#!/bin/bash) in Shell Script stand for?
What does "-xe" with Shebang (#!/bin/bash) in Shell Script stand for?

Time:07-07

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:

  1. 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 with set -x, or preferably use their more straightforward long name (example: set -o xtrace instead of set -x).
  2. Second bad practice is using the -e option or set -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:

  • Related