Score:6

Syntax error: "(" unexpected

pn flag

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!

terdon avatar
cn flag
How do you run the script? I suspect you run it with `sh` and not `bash`.
Artur Meinild avatar
vn flag
Yeah you should just run it with `./scriptname`.
Atul avatar
pn flag
running it like - sh myfilename.sh
Artur Meinild avatar
vn flag
If you're new to bash scripting, I highly recommend [this video series](https://www.youtube.com/playlist?list=PLT98CRl2KxKGj-VKtApD8-zCqSaN2mD4w) to get you started. Enjoy!
Raffa avatar
jp flag
Process substitution syntax i.e. `<(...)` works in `bash` but not in `sh`.
Score:14
vn flag

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

Raffa avatar
jp flag
+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.
Score:0
vn flag
svp

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)

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.