#!/bin/bash

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

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


当前回答

在我的例子中(在ubuntu 18.04下),我混合了$(${}),工作正常:

BACKUPED_NB=$(ls ${HOST_BACKUP_DIR}*${CONTAINER_NAME}.backup.sql.gz | wc --lines)

完整的例子。

其他回答

尝试使用bash命令显式地运行脚本,而不是将其作为可执行文件执行。

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

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

我在bash中使用花括号在表达式中添加了两次美元符号:

cp -r $PROJECT_NAME ${$PROJECT_NAME}2

而不是

cp -r $PROJECT_NAME ${PROJECT_NAME}2

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

#!/bin/sh 

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

#!/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.