bashcommand-line-interfaceshprocess-substitution

Convert /bin/bash command to /bin/sh command


I have the following command...

/bin/bash -c 'diff <(sort text2) <(sort text1)'

It sorts each file and pipes them into the diff function. This works well if /bin/bash exists. However, the system I'm on only has /bin/sh. I'm struggling to find the equivalent command for this.

If I run...

/bin/sh -c 'diff <(sort text2) <(sort text1)'

I will get...

/bin/sh: syntax error: unexpected "("


Solution

  • The POSIX shell equivalent of Bash:

    bash -c 'diff <(sort text2) <(sort text1)'
    

    is:

    sh -c 'trap "rm -fr -- \"$t\"" EXIT ABRT INT;t=$(mktemp -d);mkfifo "$t/a" "$t/b";sort text1>"$t/a"&sort text2>"$t/b"&diff "$t/b" "$t/a"'
    

    Or in a civilized way

    #!/usr/bin/env sh
    
    trap 'rm -fr -- "$tmpdir"' EXIT ABRT INT
    tmpdir=$(mktemp -d)
    
    FIFO_A="$tmpdir/fifoA"
    FIFO_B="$tmpdir/fifoB"
    mkfifo -- "$FIFO_A" "$FIFO_B"
    sort text1 > "$FIFO_A" &
    sort text2 > "$FIFO_B" &
    diff -- "$FIFO_B" "$FIFO_A"