2

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?

0

3 Answers 3

5

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, did not think of the differences between bash and bourne. This solved the problem!
@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.
1

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".

2 Comments

Thanks for the clarification, I though that #! /bin/bash at the beginning of the script would be sufficient.
@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.
0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.