minhphung
minhphung

Reputation: 41

Error "syntax error near unexpected token `(' " when add diff command in shell script

I have a problem when run diff in shellscript, it's syntax error near unexpected token('` code:

i have a 2 file File A, File B, i want compare 2 files and using in script:

diff <( sort fileA ) <( sort FileB )

but when run its get error:

syntax error near unexpected token `('

please help me! Thanks all!

Upvotes: 3

Views: 3577

Answers (1)

Adrian Fr&#252;hwirth
Adrian Fr&#252;hwirth

Reputation: 45556

Credit goes to @shellter. The construct you are using is called process substitution, which is not defined by the POSIX standard so you cannot rely on all your shells implementing this feature.

Also, when you encounter problems like this, always make sure you are actually running your script through the shell that you intend to use and if you ask a question here regarding shell scripting, mention which shell it is that you are using or that you need your problem to be targeted in, as this can make quite a difference.

Here are some examples to demonstrate that this works in e.g. bash and ksh, but not in e.g. dash:

$ bash -c 'diff <( sort file1 ) <( sort file2 )'
2c2
< file1
---
> file2

$ ksh -c 'diff <( sort file1 ) <( sort file2 )'
2c2
< file1
---
> file2

$ dash -c 'diff <( sort file1 ) <( sort file2 )'
dash: 1: Syntax error: "(" unexpected

$ sh -c 'diff <( sort file1 ) <( sort file2 )'
sh: -c: line 0: syntax error near unexpected token `('

Upvotes: 3

Related Questions