#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}

这个bash脚本在ubuntu上给了我糟糕的替换错误。任何帮助都将不胜感激。


当前回答

我发现这个问题要么是由标记的答案引起的,要么是在bash声明之前有一行或空格

其他回答

我也有同样的问题。确保你的脚本没有

#!/bin/sh 

在你的脚本顶部。相反,你应该补充

#!/bin/bash

Ubuntu下的默认shell (/bin/sh)指向dash,而不是bash。

me@pc:~$ readlink -f $(which sh)
/bin/dash

所以如果你chmod +x your_script_file.sh,然后用。/your_script_file.sh运行它,或者如果你用bash your_script_file.sh运行它,它应该工作得很好。

使用sh your_script_file.sh运行它将不起作用,因为散列行将被忽略,脚本将由dash解释,dash不支持字符串替换语法。

您的脚本语法是有效的bash和良好的。

故障可能原因:

Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash. Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.

两者- bash或dash -工作,但语法需要:

FILENAME=/my/complex/path/name.ext
NEWNAME=${FILENAME%ext}new

对于到达这里的其他命令,当使用env变量语法执行命令时,也会出现相同的消息,例如${which sh}而不是正确的$(which sh)