Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

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!

like image 831
minhphung Avatar asked Jul 04 '26 19:07

minhphung


1 Answers

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 `('
like image 90
Adrian Frühwirth Avatar answered Jul 11 '26 05:07

Adrian Frühwirth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!