Syntax error in shell script with process substitution
The syntax you've used is a bash extension to the basic shell syntax, so you must take care to run your script with bash. (Ksh also has >(…)
process substitution but doesn't support it after a redirection. Zsh would be fine.)
Given the error message you're getting, you are running this script in bash, but in its POSIX compatibility mode, not in full bash mode. Take care to invoke your script with an explicit #!/bin/bash
line. #!/bin/sh
won't do, even if /bin/sh
is a symbolic link to bash, because bash runs in POSIX mode if it's invoked under the name sh
. Always invoke bash by name if you use bash features.
Also take care not to set the environment variable POSIXLY_CORRECT
or to pass the --posix
option on the command line if you want to use bash features.
Alternatively, don't use this bash-specific syntax; use a portable construct such as the one proposed by Stephane Rouberol.
Bash script process substitution Syntax error: ( unexpected
You should run your script with bash
, i.e. either bash ./script.sh
or making use of the shebang by ./script.sh
after setting it to executable. Only running it with sh ./script.sh
do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
syntax error: ( unexpected when sh a file
Process substitution (<()
) is a bash feature. It is not supported with /bin/sh
, which guarantees only features present in the POSIX sh specification (on platforms conformant with 1992-era or newer POSIX specifications; on old ones, it could be 1970s-era Bourne).
Use bash yourscript
, or a #!/bin/bash
shebang, to run this file.
syntax error near unexpected token `(' error with process substitution
Try using sudo
:
sudo sort test.tsv > text1.tsv
sudo sort test2.tsv > text2.tsv
sudo comm -13 text1.tsv text2.tsv
-bash: command substitution: syntax error near unexpected token |'
While you have found a solution to your problem, it is worth explaining your problem. By escaping the double quotes, you prevent them from being parsed as syntactic quotes (they are interpreted as literal quotes).
Therefore, you are not echoing $vm_name
preceded and followed by empty lines : you are actually echoing "
, having empty command lines (doing nothing), and then trying to execute the content of vm_name
expanded, subject to word splitting, and then seen as a command followed by arguments (which understandably fails).
The following would have worked :
vm_name=$(curl http://artii.herokuapp.com/make?text=saml)
echo -e "
$vm_name
"
Of course, just executing the curl
command without capturing it in a variable is simpler.
Just remember escaped double quotes are not interpreted as string delimiters, they are literal double quotes.
command substitution: line 72: syntax error near unexpected token `('
Supposing that the first of these lines ...
updated_run_names=$(comm -23 <(echo ${current_run_names} | tr " " "\n" | sort) <(echo ${started_run_names} | tr " " "\n" | sort) )
update_run_names=$(echo ${new_run_names} | cut -d' ' -f1)
... is the line 72 referenced in the error message, the issue is most likely with your process substitutions. These are the two fragments of the form <( command )
.
Process substitution is a Bash extension to the POSIX shell language. It is not recognized by most other shells, and it is not recognized by Bash itself when it runs in POSIX mode. There is more than one way to get Bash running in POSIX mode, but one of them is to invoke it via the name sh
, which is what you are doing.
Note well that the shebang line at the top of your script has a functional role only when you launch the script directly, by making it executable and launching it by name:
./conversion.sh -r directory_location
When you instead launch it as shown in the question, by naming it as an argument to sh
, the shebang line is just a comment, and Bash runs (as sh
) in POSIX mode.
Note also that although it is conventional for some other language interpreters (Python, especially), it is not conventional to use env
in shell script shebang lines.
syntax error near unexpected token `'
You get the error because process substitution (the <(some command)
part) is not a standard feature (defined in POSIX) in sh
, which means it may work on some OS but may not in others or in the same OS with different configuration.
You clarified that you have #!/bin/bash
at the top of your script, but I guess you still run the script via sh foo.sh
, as such, #!/bin/bash
will be ignored and the script is interpreted by sh
.
I assume your default shell is bash
(run echo $SHELL
), so all problems are gone if you paste the script in terminal and execute.
==== UPDATE ====
Possible solution if my assumption is correct:
Leave #!/bin/bash
as it is, make your script an executable by chmod +x foo.sh
. Then run it directly by ./foo.sh
Related Topics
How to Send a HTML Email With the Bash Command "Sendmail"
How to Turn Off Echo While Executing a Shell Script Linux
How to Grep For Contents After Pattern
What Does Set -E Mean in a Bash Script
Confused About Stdin, Stdout and Stderr
Aborting a Shell Script If Any Command Returns a Non-Zero Value
Better Way to Rename Files Based on Multiple Patterns
How to Use Local Docker Images With Minikube
How to Delete a Newline If It Is the Last Character in a File
Avoid Gnome-Terminal Close After Script Execution
How to Kill All Processes With a Given Partial Name
How to Deal With a Filename That Starts With the Hyphen (-) Character
How to Open a Socket and Pass It to Another Process in Linux
Exit Code of Variable Assignment to Command Substitution in Bash
How to Split CSV Files as Per Number of Rows Specified
Recursively Look For Files With a Specific Extension
Disable Randomization of Memory Addresses
Shell Command to Tar Directory Excluding Certain Files/Folders