Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using <() in a shell script cause a syntax error?

Tags:

linux

bash

shell

I'm writing a shell script called myShellScript.sh inside of which I have the following text:

echo *** Print out this line ****
diff <(./myProgram) <(./otherProgram)

However, when I run sh myShellScript.sh I get the following error:

-bash-4.2$ sh myShellScript.sh 
myShellScript.sh **** Print out this line **** myShellScript.sh
myShellScript.sh: line 2: syntax error near unexpected token `('
myShellScript.sh: line 2: `diff <(./myProgram) <(./otherProgram)'
like image 500
Apollo Avatar asked Apr 15 '26 20:04

Apollo


2 Answers

Process substitution with the <(...) operator is a bash feature. You're getting that error message because your script is getting executed as something else (for example dash), or an older version of bash, or bash running in POSIX-compatibility mode with non-POSIX features like process substitution disabled (thanks, @chepner!)

If you want to execute the script with a full-featured bash, you have two options:

  1. Run the script with bash:

    bash myShellScript.sh 
    
  2. Set the first line of the script to #!/bin/bash (or whatever is the path to bash in your system), and run the script like this:

    ./myShellScript.sh 
    
like image 180
janos Avatar answered Apr 18 '26 10:04

janos


You need to execute your script with bash, not with sh.

You are using process substitution, which is not a standard POSIX shell feature. sh is a POSIX-compatible shell, so it does not support language extensions like process substitution. Bash will run with POSIX compatibility enabled if it is invoked as sh.

Therefore, you should execute scripts that require Bash-specific features using bash.

like image 43
nneonneo Avatar answered Apr 18 '26 10:04

nneonneo



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!