string bash:错误的替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20615217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: Bad Substitution
提问by Arindam Choudhury
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
This bash script gives me Bad substitutionerror on Ubuntu. Any help will be highly appreciated.
这个 bash 脚本在 Ubuntu 上给了我错误的替换错误。任何帮助将不胜感激。
回答by Vanni Totaro
The default shell (/bin/sh
) under Ubuntu points to dash
, not bash
.
/bin/sh
Ubuntu 下的默认 shell( ) 指向dash
,而不是bash
。
me@pc:~$ readlink -f $(which sh)
/bin/dash
So if you chmod +x your_script_file.sh
and then run it with ./your_script_file.sh
, or if you run it with bash your_script_file.sh
, it should work fine.
因此,如果您chmod +x your_script_file.sh
然后使用 运行它./your_script_file.sh
,或者如果您使用 运行它bash your_script_file.sh
,它应该可以正常工作。
Running it with sh your_script_file.sh
will not work because the hashbang line will be ignored and the script will be interpreted by dash
, which does not support that string substitution syntax.
运行它将sh your_script_file.sh
不起作用,因为 hashbang 行将被忽略并且脚本将由 解释dash
,它不支持该字符串替换语法。
回答by Guest
I had the same problem. Make sure your script didnt have
我有同样的问题。确保你的脚本没有
#!/bin/sh
at the top of your script. Instead, you should add
在脚本的顶部。相反,您应该添加
#!/bin/bash
回答by Nacho Coloma
For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh}
instead of the correct $(which sh)
对于到达这里的其他人,当使用 env 变量语法作为命令时,也会出现这个确切的消息,例如,${which sh}
而不是正确的$(which sh)
回答by P.P
Your script syntax is valid bash and good.
您的脚本语法是有效的 bash 并且很好。
Possible causes for the failure:
失败的可能原因:
Your
bash
is not really bash butksh
or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash. Dols -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
orsh script.sh
(and your default shell is not bash). Since you have proper shebang, if you have bash./script.sh
orbash ./script.sh
should be fine.
您
bash
不是真正的 bash,而是ksh
其他一些不理解 bash 参数替换的 shell。因为您的脚本看起来不错并且可以与 bash 配合使用。做ls -l /bin/bash
并检查它真的是bash,而不是符号链接到其他一些shell。如果您的系统上确实有 bash,那么您可能会以错误的方式执行脚本,例如:
ksh script.sh
或sh script.sh
(并且您的默认 shell 不是 bash)。既然你有适当的shebang,如果你有bash./script.sh
或bash ./script.sh
应该没问题。
回答by Pale Blue Dot
Try running the script explicitly using bash command rather than just executing it as executable.
尝试使用 bash 命令显式运行脚本,而不仅仅是将其作为可执行文件执行。
回答by wizurd
Also, make sure you don't have an empty string for the first line of your script.
另外,请确保脚本的第一行没有空字符串。
i.e. make sure #!/bin/bash
is the very first line of your script.
即确保#!/bin/bash
是脚本的第一行。
回答by Hagen
Both - bash or dash - work, but the syntax needs to be:
两者 - bash 或 dash - 都可以工作,但语法需要是:
FILENAME=/my/complex/path/name.ext
NEWNAME=${FILENAME%ext}new
回答by Daniel Darabos
Not relevant to your example, but you can also get the Bad substitution
error in Bash for any substitution syntax that Bash does not recognize. This could be:
与您的示例无关,但是Bad substitution
对于 Bash 无法识别的任何替换语法,您也可以在 Bash 中获得错误。这可能是:
- Stray whitespace. E.g.
bash -c '${x }'
- A typo. E.g.
bash -c '${x;-}'
- A feature that was added in a later Bash version. E.g.
bash -c '${x@Q}'
before Bash 4.4.
- 杂散的空白。例如
bash -c '${x }'
- 一个错字。例如
bash -c '${x;-}'
- 在更高版本的 Bash 中添加的功能。例如
bash -c '${x@Q}'
在 Bash 4.4 之前。
If you have multiple substitutions in the same expression, Bash may not be very helpful in pinpointing the problematic expression. E.g.:
如果您在同一个表达式中有多个替换,Bash 可能无法帮助确定有问题的表达式。例如:
$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution
回答by sashoalm
I was adding a dollar sign twice in an expression with curly braces in bash:
我在 bash 中带花括号的表达式中添加了两次美元符号:
cp -r $PROJECT_NAME ${$PROJECT_NAME}2
instead of
代替
cp -r $PROJECT_NAME ${PROJECT_NAME}2
回答by Andrew Knutsen
Looks like "+x" causes problems:
看起来像“+x”会导致问题:
root@raspi1:~# cat > /tmp/btest
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
root@raspi1:~# chmod +x /tmp/btest
root@raspi1:~# /tmp/btest
root@raspi1:~# sh -x /tmp/btest
+ jobname=job_201312161447_0003
/tmp/btest: 4: /tmp/btest: Bad substitution