3

I'm testing a simple script and I'm wondering why it works fine when executed from directory: ./test.sh but when I try with "sh" command sh test.sh it's not working:

test.sh: 3: test.sh: [[: not found
test.sh: 7: test.sh: [[: not found

Script:

#!/usr/bin/env bash

if [[ $1 = one ]]
        then
        printf "%b" "two\n" >&2
        exit 0
elif [[ $1 = two ]]
then
        printf "%b" "one\n" >&2
        exit 0
else
        printf "%b" "Specify argument: one/two\n"
        exit 1
fi

2 Answers 2

15

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

2
  • 3
    Also, sh is not always the Bourne shell, but usually implies compatibility with POSIX sh (based largely on Bourne). On Ubuntu, for instance, /bin/sh is symlinked to /bin/dash. Jun 21, 2012 at 22:12
  • sh is bash on CentOS and dash on Ubuntu.
    – user9517
    Jun 21, 2012 at 22:14
3

As noted: /bin/sh typically (though not always) invokes a POSIX-compliant Bourne shell. Bash is Not Bourne.

Bash will attempt to emulate Bourne when invoked as 'sh' (as when /bin/sh symlinks or links to /bin/bash), or if $POSIXLY_CORRECT is defined in the invoking environment, when invoked with the --posix invocation option, or when 'set -o posix' has been executed. This enables testing a Bourne shell script / command for POSIX compliance.

Alternately, invoke scripts / test commands with a known POSIX-compliant shell. 'dash' is close, the Korn shell (ksh) IIRC offers a POSIX compliant option as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .