6

While executing the below command directly over the terminal, its working fine:

comm -2 -3 <(sort FileOne.txt) <(sort FileTwo.txt) > myFile.txt

But while trying to execute it via .sh file, its giving error: Syntax error: "(" unexpected

Here is my code:

#!/bin/bash
##this will return the unique lines from the first file.
comm -2 -3 <(sort FileOne.txt) <(sort FileTwo.txt) > myFile.txt

I run the script like this:

sh ./myfilename.sh

Please help me to solve it.

Thanks!

5
  • 3
    How do you run the script? I suspect you run it with sh and not bash.
    – terdon
    Jan 9 at 13:21
  • Yeah you should just run it with ./scriptname. Jan 9 at 13:22
  • running it like - sh myfilename.sh
    – Atul
    Jan 9 at 13:25
  • If you're new to bash scripting, I highly recommend this video series to get you started. Enjoy! Jan 9 at 16:11
  • Process substitution syntax i.e. <(...) works in bash but not in sh.
    – Raffa
    Jan 10 at 13:54

2 Answers 2

14

If needed, make the script executable first by running:

chmod +x myfilename.sh

Then, run the script like this:

./myfilename.sh

Or run it explicitly with bash like this:

bash myfilename.sh

(These first two commands are essentially the same, since you have the bash shebang #!/bin/bash at the beginning of your script.)

DO NOT run like this:

sh myfilename.sh

Because then you are running the script with Dash (sh) and not with Bash (since your script has Bash-specific syntax).

1
  • +1 ... It might be worth noting that chmod +x is safer(ish) as it respects your environment's set umask(file mode creation mask) ... chmod a+x on the other hand should only be used if you mean it as it will set the executable bit for all(owner, group and others) regardless of the umask value.
    – Raffa
    Jan 10 at 13:33
0

The answer is similar to answer already posted, but giving an explanantion.

I thing you are running this script sh code.sh. This means you are using sh to run the script, but the first line (#!/bin/bash) implies it's been written for bash.

On some systems sh and bash are the same, but on others they are not; and, when invoked as sh, Bash turns off some non-POSIX features. So it's important to use the right shell and the right invocation.

Use bash code.sh or better still, make the script executable (chmod a+x code.sh) and then run it directly (./code.sh)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.