1

I'm running this one liner as part of a larger script-

paste -d '' <(cut -c"1-5" file1) <(cut -c"1-2" file2) <(cut -c"8-" file1)

This takes the first 5 characters of the first file file1, the characters 1-2 of file2 and then the characters 8 onwards of file1 and pastes them all together.

$cat file1
1234567890
1234567890
1234567890
1234567890
1234567890

$cat file2
abcdefghij
abcdefghij
abcdefghij
abcdefghij
abcdefghij

output-
12345ab890
12345ab890
12345ab890
12345ab890
12345ab890

This works perfectly as expected from the command line (output shown above).

However if I put that line into a script (shown here)-

$cat a.sh

#!/bin/bash
paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)

If I run the script I get this error-

$sh a.sh
a.sh: line 2: syntax error near unexpected token `('
a.sh: line 2: `paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)'

Any ideas what is going wrong here? I ran it through shellcheck and it told me the script was fine.

Chem-man17
  • 1,700
  • 1
  • 12
  • 27
  • 3
    You run your script with `sh`, and `sh` doesn't know about process substitution (the `<()` syntax). Run it with either `bash a.sh`, or make it executable (`chmod +x a.sh`) and run it with `./a.sh`. Also, the first line of `a.sh` should not be empty, or the shebang line (`#!/bin/bash`) could be ignored. – Benjamin W. Oct 26 '16 at 13:59
  • 1
    As an aside, your prose description doesn't agree with the code at all. For example, where you have `cut -c1-2` your description says you want characters 3-10. – tripleee Oct 26 '16 at 14:38
  • @tripleee, that's what I get for first using a real example from my work and then trying to make it simpler for the question. Thanks for the heads up! – Chem-man17 Oct 26 '16 at 14:43

1 Answers1

1

run as below;

./a.sh 

or

bash a.sh

bash and sh are two different shells. bash is with more features and better syntax.

Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24