不从脚本粘贴 运行 但在命令行上工作

paste not running from script but working on command line

我运行将这个衬里作为更大脚本的一部分-

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

这会获取第一个文件 file1 的前 5 个字符、file2 的第 1-2 个字符以及 file1 的第 8 个字符并将它们全部粘贴在一起。

$cat file1
1234567890
1234567890
1234567890
1234567890
1234567890

$cat file2
abcdefghij
abcdefghij
abcdefghij
abcdefghij
abcdefghij

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

这完全符合命令行的预期(如上所示的输出)。

但是,如果我将该行放入脚本(此处显示)-

$cat a.sh

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

如果我 运行 脚本我得到这个错误-

$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)'

知道这里出了什么问题吗?我通过 shellcheck 运行 它告诉我脚本没问题。

运行如下;

./a.sh 

bash a.sh

bash 和 sh 是两个不同的 shell。 bash 具有更多功能和更好的语法。