Why doesn't process substitution work in a shell script?

I try to execute the command command in a shell script, but I get 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' 

code snippet -

 temp=`comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l` echo $temp 
+9
linux bash shell awk
source share
1 answer

It is not yet clear, but the chances are very high that you either have the wrong Shebang line at the top of the script:

 #!/bin/sh 

either you use sh script.sh instead of bash script.sh during testing, or you have SHELL=/bin/sh or something similar in the environment. Your error is related to the process replacement code. When Bash starts as sh (in POSIX mode ), process substitution is not available:

  1. Substitution process 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 just:

 #!/bin/bash comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l 

which achieves the same effect as a capture followed by an echo. When testing, use bash -x script.sh or bash script.sh .


Deciphering a illegible comment

In a illegible (and now deleted) comment, this information apparently includes:

BASH = / bin / w
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-comment: POSIX
SHLVL = 2

Note that BASH=/bin/sh and SHELLOPTS=braceexpand:hashall:interactive-comments:posix . Any or both of these can be a major part of the problem.

+8
source share

All Articles