#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
这个bash脚本在ubuntu上给了我糟糕的替换错误。任何帮助都将不胜感激。
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
这个bash脚本在ubuntu上给了我糟糕的替换错误。任何帮助都将不胜感激。
当前回答
与您的示例无关,但是对于Bash无法识别的任何替换语法,您也可以在Bash中获得Bad替换错误。这可能是:
流浪的空白。例如bash -c '${x}' 一个错字。例如bash -c '${x;-}' 在后来的Bash版本中添加的一个特性。例如bash 4.4之前的bash -c '${x@Q}'。
如果在同一个表达式中有多个替换,Bash在确定有问题的表达式时可能没有多大帮助。例如:
$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution
其他回答
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命令显式地运行脚本,而不是将其作为可执行文件执行。
另外,确保脚本的第一行没有空字符串。
即确保#!/bin/bash是脚本的第一行。
您的脚本语法是有效的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无法识别的任何替换语法,您也可以在Bash中获得Bad替换错误。这可能是:
流浪的空白。例如bash -c '${x}' 一个错字。例如bash -c '${x;-}' 在后来的Bash版本中添加的一个特性。例如bash 4.4之前的bash -c '${x@Q}'。
如果在同一个表达式中有多个替换,Bash在确定有问题的表达式时可能没有多大帮助。例如:
$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution