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.
Why does process substitution not work in a shell script?
It isn't entirely clear yet, but the chances are very high that you either have an incorrect shebang line at the top of the script:
#!/bin/sh
or you are using sh script.sh
instead of bash script.sh
while testing it, or you have SHELL=/bin/sh
or something similar set in the environment. Your failure is on the process substitution code. When Bash is run as sh
(in POSIX mode), then process substitution is not available:
- Process substitution is not available.
You need to write:
#!/bin/bash
temp=$(comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l)
echo $temp
or even simply:
#!/bin/bash
comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l
which will achieve the same effect as the capture followed by the echo. When testing, use bash -x script.sh
or bash script.sh
.
Deciphering the indecipherable comment
In an indecipherable comment, the information appears to include:
BASH=/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="a.sh")
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
CVS_RSH=ssh
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2
Note that BASH=/bin/sh
and SHELLOPTS=braceexpand:hashall:interactive-comments:posix
. Either or both of these might be a major part of the problem.
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
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.
bash/zsh input process substitution gives syntax error in conjunction with while
You need to redirect the process substitution into the while loop:
You wrote
while read line; do echo $line; done <(cat foo)
You need
while read line; do echo $line; done < <(cat foo)
# ...................................^
Treat a process substitution like a filename.
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
process substitution not working in bash script
Process substitution does not work when bash is in POSIX mode. Please disable POSIX and try again.
To disable: This will cause process substitution to work .
set +o posix
To enable: : This will cause process substitution not to work.
set -o posix
Related Topics
How to Tar a Directory Without Retaining the Directory Structure
How to Convert Dos/Windows Newline (Crlf) to Unix Newline (Lf)
How to Write Standard Error to a File While Using "Tee" With a Pipe
Environment Variable Substitution in Sed
Cannot Connect to the Docker Daemon At Unix:/Var/Run/Docker.Sock. Is the Docker Daemon Running
Use Sudo With Password as Parameter
How to Compile a 32-Bit Binary on a 64-Bit Linux Machine With Gcc/Cmake
How to Run a Program With a Different Working Directory from Current, from Linux Shell
Run an Untrusted C Program in a Sandbox in Linux That Prevents It from Opening Files, Forking, etc.
Edit Shell Script While It's Running
Change the X-Frame-Options to Allow All Domains
What Does Set -E Mean in a Bash Script
Using Printf in Assembly Leads to Empty Output When Piping, But Works on the Terminal
Finding Which Process Was Killed by Linux Oom Killer
Is There Any API For Determining the Physical Address from Virtual Address in Linux
How to Set Environment Variable For Everyone Under My Linux System