Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question has appeared on a pre-interview quiz and it's making me crazy. Can anyone answer this and put me at ease? The quiz has no reference to a particular shell but the job description is for a unix sa. again the question is simply...

What does 'set -e' do, and why might it be considered dangerous?

share|improve this question
Here's a related thread:… – Stefan Lasiewski May 20 '10 at 20:26
up vote 88 down vote accepted

set -e causes the shell to exit if any subcommand or pipeline returns a non-zero status.

The answer the interviewer was probably looking for is:

It would be dangerous to use "set -e" when creating init.d scripts:

From 9.3.2 --

Be careful of using set -e in init.d scripts. Writing correct init.d scripts requires accepting various error exit statuses when daemons are already running or already stopped without aborting the init.d script, and common init.d function libraries are not safe to call with set -e in effect. For init.d scripts, it's often easier to not use set -e and instead check the result of each command separately.

This is a valid question from an interviewer standpoint because it gauges a candidates working knowledge of server-level scripting and automation

share|improve this answer
nice find. Thanks. – egorgry Aug 20 '12 at 13:15
good point. it would halt the boot process over something that might be an error technically, but shouldn't cause the whole script to stop – Matt Sep 19 '12 at 2:29

From bash(1):

          -e      Exit immediately if a pipeline (which may consist  of  a
                  single  simple command),  a subshell command enclosed in
                  parentheses, or one of the commands executed as part  of
                  a  command  list  enclosed  by braces (see SHELL GRAMMAR
                  above) exits with a non-zero status.  The shell does not
                  exit  if  the  command that fails is part of the command
                  list immediately following a  while  or  until  keyword,
                  part  of  the  test  following  the  if or elif reserved
                  words, part of any command executed in a && or  ││  list
                  except  the  command  following  the final && or ││, any
                  command in a pipeline but the last, or if the  command’s
                  return  value  is being inverted with !.  A trap on ERR,
                  if set, is executed before the shell exits.  This option
                  applies to the shell environment and each subshell envi-
                  ronment separately (see  COMMAND  EXECUTION  ENVIRONMENT
                  above), and may cause subshells to exit before executing
                  all the commands in the subshell.

Unfortunately I'm not creative enough to think of why it would be dangerous, other than "the rest of the script won't get executed" or "it might possibly perhaps mask real problems".

share|improve this answer
mask real/other problems, yeah, i guess i could see that... i was struggling to come up with an example of it being dangerous, as well... – cpbills May 19 '10 at 16:48
There's the manpage for set! I always end up at builtin(1). Thanks. – Jared Beck May 29 '13 at 23:27

Keep in mind this is a quiz for a job interview. The questions may have been written by the current staff, and they may be wrong. This isn't necessarily bad, and everyone makes mistakes, and interview questions often sit in a dark corner without review, and only come out during an interview.

It's entirely possible that 'set -e' does nothing that we would consider "dangerous". But the author of that question may mistakenly believe that 'set -e' is dangerous, due to their own ignorance or bias. Maybe they wrote a buggy shell script, it bombed horribly, and they mistakenly thought that 'set -e' was to blame, when in fact they neglected to write proper error checking.

I've participated in probably 40 job interviews over the last 2 years, and the interviewers sometimes ask questions which are wrong, or have answers which are wrong.

Or maybe it's a trick question, which would be lame, but not entirely unexpected.

Or maybe this is a good explanation:

share|improve this answer
+1 i'm in the same boat, and a lot of the 'technical' interviews i've been dealing with have been at least slightly misguided. – cpbills May 19 '10 at 17:43
wow. Good find on the debian list. +1 for the ignorance of interview questions. I remember arguing a netback answer once since I was 100% sure I was right. They said nope. I went home and googled it, I was right. – egorgry May 19 '10 at 18:48

It should be noted that set -e can be turned on and off for various sections of a script. It doesn't have to be on for the whole script's execution. It could even be conditionally enabled. That said, I don't ever use it since I do my own error handling (or not).

some code
set -e     # same as set -o errexit
more code  # exit on non-zero status
set +e     # same as set +o errexit
more code  # no exit on non-zero status

Also noteworthy is this from the Bash man page section on the trap command which also describes how set -e functions under certain circumstances.

The ERR trap is not executed if the failed command is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a command executed in a && or ⎪⎪ list, or if the command's return value is being inverted via !. These are the same conditions obeyed by the errexit option.

So there are some conditions under which a non-zero status will not cause an exit.

I think the danger is in not understanding when set -e comes into play and when it doesn't and relying on it incorrectly under some invalid assumption.

share|improve this answer

set -e tells bash, in a script, to exit whenever anything returns a non-zero return value.

i could see how that would be annoying, and buggy, not sure about dangerous, unless you had opened up permissions on something, and before you could restrict them again, your script died.

share|improve this answer
That's interesting. I didn't think about permissions opened up in a script that dies prematurely but I could see that being considered dangerous. The fun part of this quiz is that you can't use any reference materials such as man or google and if you can't answer fully don't answer it at all. – egorgry May 19 '10 at 17:02
that's just silly, i would reconsider this employer... kidding (sort of). it's good to have a strong base of knowledge, but half of IT work is knowing /where/ to find the information... hopefully they were smart enough to take that into consideration when scoring applicants. on a side note, i see /no/ use for set -e, in fact, to me it speaks of laziness to ignore error checking in your scripts... so keep that in mind, with this employer, too... if they use it a lot, what do their scripts look like, that you will inevitably have to maintain... – cpbills May 19 '10 at 17:45
excellent point @cpbills. I also see no use for set -e in a script and it's probably why it stumped me so much. I'd like to post all the questions since some are really good and some are really wacky and random like this one. – egorgry May 19 '10 at 18:46
I've seen it in many system cron jobs... – Andrew May 20 '10 at 0:29

I'd say it's dangerous because you don't control he flow of your script anymore. The script can terminate as long as any of the commands that the script invokes returns a non-zero. So all you have to do is to do something that alters the behavior or output of any of the components, and you get to terminate the main script. It might be more of a style problem, but it definitely has consequences. What if that main script of yours supposed to set some flag, and it didn't because it terminated early? You'd end up faulting the rest of the system if it assumes the flag should be there, or working with an unexpected default or old value.

share|improve this answer
Totally agree with this answer. It's not inherently dangerous, but it's an opportunity to have an unexpected early exit. – pboin May 20 '10 at 0:18
What this reminded me of is a C++ prof of mine that was always pulling points from my assignments for not having single entry point/single exit point in a program. I thought it was a purely a 'principle' thing, but this set -e business definitely demos how and why things can completely spin out of control. Ultimately programs are about control and determinism, and with premature termination you give up both. – Marcin May 20 '10 at 11:45

As a more concrete example of @Marcin's answer which has personally bitten me, imagine there was a rm foo/bar.txt line somewhere in your script. Usually no big deal if foo/bar.txt doesn't actually exist. However with set -e present now your script will terminate early there! Oops.

share|improve this answer

Your Answer


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.