tags:

views:

244

answers:

3

Hi,

I have the following question:

When I execute the following script directly in a terminal window, the commands behave as expected.

$ diff <(echo tmp) <(echo tmp1)
1c1
< tmp
---
> tmp1

However when I write the same command in a shell script

#! /bin/bash
diff <(echo tmp) <(echo tmp1)

I get the following error message:

$ sh test.sh
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: ` diff <(echo tmp) <(echo tmp1)'

Initially I thought this was an issue with diff, but this also happens with other commands. Does anybody have an idea what causes the problem?

+2  A: 

Try

bash test.sh

or

chmod ugo+x test.sh
./test.sh

Works fine for me when I do either.

Looks like the syntax is not supported by the bourne shell (sh).

codaddict
Thanks, did not think of the differences between bash and bourne. This solved the problem!
sugo
@sugo: If your problem is solved, do consider accepting codaddict's answer. There's a tick outline under the answer's score you click on to do that.
Charles Stewart
A: 

That syntax doesn't look familiar. Are you sure you are using bash in your terminal? You can verify by typing echo $SHELL.

soulmerge
I have this working as per qn on a bash on osx, and failing for a bash on debian, both version 2.05b.0(1)-release.
Charles Stewart
A: 

When bash is invoked using sh, it starts up in a special, POSIX-compliant mode. This has different syntax, which I guess explains the different results.

See bashref of POSIX mode, #22: "process substitution is not available".

Charles Stewart
Thanks for the clarification, I though that #! /bin/bash at the beginning of the script would be sufficient.
sugo
@sugo: The shebang is overridden when you specify the interpreter. It isn't necessarily running Bash in POSIX mode unless `sh` is symlinked to `bash`. So it's actually using whatever `sh` represents on your system. One way to see this is to create a file consisting of `ls -l /proc/$$/exe` called "showshell" and run it with `sh ./showshell` and `bash ./showshell`, and you'll see the symlink. On my system, `sh` is `/bin/dash`.
Dennis Williamson