The following is a script I wrote on a Mac (using the bourne shell "sh".) This is part of a larger script.

The goal: I have a zip file where I know the names of the zipped files ahead of time. So I want to unzip the zip file into /tmp/sampleFolder. I then want to be able to check that folder to ensure the filenames I expect are in there.

For example, if one of the files in the zip is john.txt, I want to check that



The problem: Oddly enough, my check for the file's existence seems to take place BEFORE the unzip is complete. I can fix this by adding set -e at the top of the script. Then all the commands in the script seem to execute sequentially. But I can't understand why this fixes it.

I would appreciate:

a.) Alternative solutions / explanation of why the check for file existence occurs before file gets unzipped

b.) or an explanation of why set -e forces the script to execute sequentially. My research into set -e simply says the script is forced to exit if commands make a return other than 0. However, instead of merely exiting, the script runs sequentially. So it seems there is something more to it. Thank you!


rm -rf "/tmp/sampleFolder"

unzip ~/Desktop/myFile.zip -d "/tmp/sampleFolder"

if [ ! -r /tmp/sampleFolder/john.txt ]; then
echo "file not present!"
  • You say this is part of a bigger script. The problem (if there is one) is likely to be in the bigger script, and not present in the simplified example. – janos Jan 30 '17 at 18:31
  • I can duplicate the issue with the exact script i posted janos. So if that was your minus, I'd ask you to reconsider. – Mark Purpelio Jan 30 '17 at 18:37
  • Interesting! (And no, I didn't downvote.) – janos Jan 30 '17 at 18:39
  • 1
    @janos I am honest to enough to admit when I am wrong because it helps us to get to the right answers. I was wrong! I thought I had reproduced the issue with my sample code, but I didn't. You were correct. The problem was elsewhere in the code. In my case, I was launching the script from macOS Automator, and it was being run twice in rapid succession. That meant when the second run took place, it deleted the file just before the first run of the script was looking to confirm that it still existed. If you make your note an answer I will select it as correct. – Mark Purpelio Jan 30 '17 at 21:00
  • I appreciate your honesty :-) – janos Jan 30 '17 at 21:11

I see no way for the statements in the posted script to get executed non-sequentially.

The following is a script I wrote on a Mac (using the bourne shell "sh".) This is part of a larger script.

If there is really a problem, it's likely to be in the bigger script, that's why it's not present in the simplified example.


Shell scripts always run in order. Set -e has no effect on order of operations, only exiting on nonzero.

The exception to 'in order' is when you background tasks with &, which does not appear to be present here.

I suspect your unzip is not behaving quite like you expect. First try echoing the return code of unzip with echo $? on the next line and see if it is succeeding. After that add a ls -l /tmp/sampleFolder to see the contents.
You can also run set -x to see trace output from bash which will also show the order the of operations in the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy