Qiang Li
Qiang Li

Reputation: 10855

shell error with "-bash: bad substitution: no closing `)'"

I have this script (just copy and paste it into shell)

perl -c <(cat <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF
)

On one linux machine, I got /dev/fd/63 syntax OK output, which is ok. But on macbook terminal, I saw this

$ perl -c <(cat <<'EOF'
> #!/usr/bin/perl
> while( @mylist>1){
> if($i > $initnum) {$i--;}
>    {splice( @mylist,1);}
> }
> EOF
> )
-bash: bad substitution: no closing `)' in <(cat <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   splice( @mylist
}
EOF
)

My question is why such error. And ideally how to fix it on mac.

Upvotes: 3

Views: 2460

Answers (2)

bishop
bishop

Reputation: 39364

Likely an incompatible version (or broken version as @chepner commented) of bash on your MacOS, so you can naively rewrite it with echo:

echo '#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
    {splice( @mylist,1);}
}
' | perl -c

Or if you need to keep the cat business, you can bust it up into a pipeline:

cat <<'EOF' | perl -c
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF

Upvotes: 2

chepner
chepner

Reputation: 531045

It's a parser bug in bash 3.2, but your example is a quite torturous replacement for a simple here document:

perl -c <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF

It does not seem likely that Apple will ever ship a newer version of bash by default, so your best bet is to install one yourself (via Homebrew, for example).

Upvotes: 3

Related Questions