Issue
I have the following bash script test.sh (with execution permissions):
#!/bin/sh
CAT_BIN="cat"
"$CAT_BIN" <(tail -n +2 test.sh)
It gives me that error when I run it:
$ ./test.sh
./test.sh: line 4: syntax error near unexpected token `('
./test.sh: line 4: `"$CAT_BIN" <(tail -n +2 test.sh)'
However, when I source the following commands it executes alright.
$ CAT_BIN="cat"
$ "$CAT_BIN" <(tail -n +2 test.sh)
How can this work in a script? (Use <(tail -n +2 test.sh)
inline as a filename argument)
Solution
The <(tail -n +2 test.sh)
construct is a bash feature, so you need to run your script in the bash
shell,
Replace your top line
#!/bin/sh
with
#!/bin/bash
(Or the proper path to the bash executable if it is not /bin/bash
on your system)
Note, even if /bin/sh
is e.g. a symlink to bash, it will start bash in posix compatibility mode when you run it as /bin/sh
, and many bash specific features will not be available)
Answered By - nos