Summary
sh
is a different program than bash
.
Detail
The problem is that the Bourne shell (sh
) is not the Bourne Again shell (bash
). Namely, sh doesn't understand the [[
pragma. In fact, it doesn't understand [
either. [
is an actual program or link to /bin/test (or /usr/bin/[, /usr/bin/test).
$ which [
/bin/[
$ ls -lh /bin/[
-r-xr-xr-x 2 root wheel 42K Feb 29 17:11 /bin/[
When you execute your script directly through ./test.sh
, you're calling the script as the first argument to the program specified in the first line. In this case:
#!/usr/bin/env bash
Often, this is directly the interpreter (/bin/bash
, or any number of other script interpreters), but in your case you're using env to run a program in a modified environment -- but that follow argument is still bash. Effectively, ./test.sh
is bash test.sh
.
Because sh
and bash
are different shells with different syntax interpretations, you're seeing that error. If you run bash test.sh
, you should see what is expected.
More info
Others have pointed out in comments that /bin/sh
can be a link or other shell. Historically, sh
was the Bourne shell on the old AT&T Unix, and in my mind the canonical descent. However, that is different in BSD variations and has diverged in other Unix based systems and distributions over time. If you're really interested in the inner workings (including how /bin/sh and /bin/bash can be the same program and behave totally differently), read the following:
SuperUser: What is the difference between bash and sh
Wikipedia: Bourne shell