Kaitlyn
Kaitlyn

Reputation: 241

sh: syntax error near unexpected token `<'

I am creating a script where I am redirecting the output of an awk command into a loop, but it's giving me this error:

sh: -c: line 3: syntax error near unexpected token `<'
sh: -c: line 3: `done < <(awk "{print}" inputfile.txt)'

I was able to run it against a different remote host, but this host is giving me an error. Does anyone know if some versions of sh/bash don't support that syntax, or know any alternatives to that syntax, or maybe spot a bug that I haven't been able to see? (I'm new to bash scripting, so even a point in the right direction would be helpful!)

Here is a pared-down version of my script that I was able to reproduce the issue on:

#!/usr/bin/env bash

host=$1

ssh $host 'while read line
do
echo "hi";
done < <(awk "{print}" inputfile.txt)'

Upvotes: 0

Views: 2743

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52506

It looks like the remote user on that host uses a shell that's not Bash to run the command, see this Q&A; a way around that is to either avoid Bashisms:

ssh "$host" 'awk "{print}" inputfile.txt \
    | while IFS= read -r line; do
        echo "$line"
    done'

which comes with its own pitfalls, see A variable modified inside a while loop is not remembered – alternatively, you could specify that the remote host should run Bash:

ssh "$host" <<'EOF'
bash -c 'while IFS= read -r line; do
    echo "$line"
done < <(awk "{print}" inputfile.txt)'
EOF

Upvotes: 1

Related Questions