4

In terminal (bash) the following works fine:

cat <(echo "hello") 

But if I do:

sh -c 'cat <(echo "hello")'

I get

sh: 1: Syntax error: "(" unexpected

Can you explain the reason why?

Btw, my overall aim is to write this command in a shell script:

watch -n 1 'cat <(iptables -L INPUT) <(iptables -L FORWARD)'

but it won't work, the reason seems to be the above problem.

Markus
  • 578
  • 6
  • 26

2 Answers2

3

sh is often dash not bash (see man sh). dash doesn't do process substitution, only POSIX stuff.

You'll need to do:

  bash -c 'cat <(echo "hello")'

ksh & zsh can do process substitution too.

With your example, you can simply do:

watch -n 1  'iptables -L INPUT;  iptables -L FORWARD'

no need for an advanced shell or process subtitution.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

The reason for the syntax error is that sh is not linked to bash on your system and does not understand process substitution, which is not part of the POSIX standard.

I would recommend the following command which is much cleaner and works with any POSIX compatible shell:

while true ; do
    clear
    iptables -L INPUT
    iptables -L FORWARD
    sleep 1
done
hek2mgl
  • 152,036
  • 28
  • 249
  • 266