Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have some bash scripts I have setup that mostly use

#!/bin/bash

but I regularly come across some that look like

#!/bin/bash -e
#!/bin/bash -x
#!/bin/bash -ex

and so on.

Can someone explain the meaning and benefits of these shebang options and whether they apply to other shebangs?

share|improve this question
    
Those options are specific to Bash (or other interpreter). They may be the same for other shells (dash and ksh, for example), but they would be different for other interpreters such as AWK and Python. You can use many of the options that the interpreter accepts. The options are interpreter-specific while the shebang is a kernel feature. –  Dennis Williamson Oct 4 '10 at 21:26
add comment

2 Answers

up vote 26 down vote accepted

If a script /path/to/foo begins with #!/bin/bash, then executing /path/to/foo arg1 arg2 is equivalent to executing /bin/bash /path/too/foo arg1 arg2. If the shebang line is #!/bin/bash -ex, it is equivalent to executing /bin/bash -ex /path/too/foo arg1 arg2. This feature is managed by the kernel.

Note that you can portably have only one argument on the shebang line: some unices (such as Linux) only accept one argument, so that #!/bin/bash -e -x would lead to bash receiving the single five-character argument -e -x (a syntax error) rather than two arguments -e and -x.

For the Bourne shell sh and derived shells such as POSIX sh, bash, ksh, and zsh:

  • -e means that if any command fails (which it indicates by returning a nonzero status), the script will terminate immediately.
  • -x causes the shell to print an execution trace.

Other programs may understand these options but with different meanings.

share|improve this answer
add comment

They are options passed to bash see help set for more info, in this case:

-x  Print commands and their arguments as they are executed.
-e  Exit immediately if a command exits with a non-zero status.
share|improve this answer
2  
+1 And -ex does both –  Nifle Oct 4 '10 at 21:24
    
It's confusing because they look like command-line options passed into Bash. –  Caoilte Sep 19 '12 at 16:39
1  
@Caoilte: And indeed they are (from man bash): In addition to the single-character shell options documented in the description of the set builtin command, bash interprets the following options when it is invoked: [...]. –  cYrus Sep 19 '12 at 17:15
1  
arrgh! blink and you miss it! :). –  Caoilte Sep 19 '12 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.