I am trying to comm command in shell script but getting an error:
a.sh: command substitution: line 1: syntax error near unexpected token `('
a.sh: command substitution: line 1: `comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l'
snippet of code-
temp=`comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l`
echo $temp
In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell.
Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression. Example: In the below program we have firstly created variable num and assigned it with the value 20 and then substituted the value of the num as 100 in the echo command.
Command substitution allows the output of a command to replace the command itself. Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
Command substitution allows you to capture the output of any command as an argument to another command. When you place a command line within backquotes ( `` ), the shell first runs the command or commands and then replaces the entire expression, including the backquotes, with the output.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With