Ari*_*ury 116 string bash ubuntu substitution
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
Run Code Online (Sandbox Code Playgroud)
这个bash脚本在Ubuntu上给了我错误的替换错误.任何帮助将受到高度赞赏.
Van*_*aro 169
/bin/shUbuntu下的默认shell()指向dash,而不是bash.
me@pc:~$ readlink -f $(which sh)
/bin/dash
Run Code Online (Sandbox Code Playgroud)
因此,如果你chmod +x your_script_file.sh然后运行它./your_script_file.sh,或者如果你运行它bash your_script_file.sh,它应该工作正常.
运行它将sh your_script_file.sh无法工作,因为将忽略hashbang行并且脚本将被解释dash,这不支持该字符串替换语法.
小智 57
我有同样的问题.确保你的脚本没有
#!/bin/sh
Run Code Online (Sandbox Code Playgroud)
在脚本的顶部.相反,你应该添加
#!/bin/bash
Run Code Online (Sandbox Code Playgroud)
Nac*_*oma 23
对于到达此处的其他人,当使用命令的env变量语法时,也会出现此确切消息,例如,${which sh}而不是正确的$(which sh)
P.P*_*.P. 20
你的脚本语法是有效的bash和good.
失败的可能原因:
你bash不是真正的bash,ksh或者是其他一些不理解bash参数替换的shell.因为你的脚本看起来很好并且可以使用bash.做ls -l /bin/bash并检查它真的是bash而不是sym链接到其他shell.
如果你的系统上有bash,那么你可能会以错误的方式执行你的脚本:ksh script.sh或者sh script.sh(你的默认shell不是bash).因为你有适当的shebang,如果你有bash ./script.sh或bash ./script.sh应该没事.
尝试使用bash命令显式运行脚本,而不是仅将其作为可执行文件执行.
与您的示例无关,但是Bad substitution对于 Bash 无法识别的任何替换语法,您也可以在 Bash 中获得错误。这可能是:
bash -c '${x }'bash -c '${x;-}'bash -c '${x@Q}'在 Bash 4.4 之前。如果您在同一个表达式中有多个替换,Bash 可能无法帮助确定有问题的表达式。例如:
$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution
Run Code Online (Sandbox Code Playgroud)