【发布时间】:2014-05-09 13:21:16
【问题描述】:
当我运行以下脚本时:
#!/bin/bash
test="abcdr"
test2=${test:0:2}
echo $test2
我收到错误:script: 3: script: Bad substitution
然后我在终端中输入完全相同的命令,它工作正常,没有任何错误
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
你是如何运行你的脚本的?
bash在/bin/bash中吗? (检查which bash)。 -
如何运行脚本?
-
@user3597432 使其可执行 (
chmod u+x script) 并像./script一样运行它或像bash script一样运行它。sh不支持子字符串。 -
这就是问题所在,在执行
sh script时,您正在使用sh来解释它,而shell 不接受这种替换。相反,使用./script执行,将得到/bin/bash。 -
@jaypal 谢谢,就是这样