Search code examples
bashshiptables

sh -c and process substitution


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.


Solution

  • 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.