Linux bash 中的字符串替换 - 错误替换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8960677/
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
String replacement in bash - bad substitution error
提问by Aristarhys
I newbie in bash scripting but i don't uderstand why it's not work
我是 bash 脚本的新手,但我不明白为什么它不起作用
#!/bin/bash
foo=foobarfoobar
echo ${foo//bar/baz}
bad substitution error on line 3
第 3 行的错误替换错误
采纳答案by Chris
That substitution works fine in Bash 4.2.8 (and looks fine according to the documentation).
该替换在 Bash 4.2.8 中运行良好(根据文档看起来不错)。
My best guess would be that you're not actually using Bash - how are you invoking the script? If you're doing sh script.sh
you may well be running it with Dash or something similar (and Dash does indeed give a substitution error on line 3). Try explicitly running it with Bash (bash script.sh
).
我最好的猜测是你实际上并没有使用 Bash - 你是如何调用脚本的?如果你正在这样做,sh script.sh
你很可能会用 Dash 或类似的东西运行它(而且 Dash 确实在第 3 行给出了一个替换错误)。尝试使用 Bash ( bash script.sh
)显式运行它。
If it turns out you are actually using Dash, there's some useful information on the differences and how to go back to using Bash (if you want to) here: https://wiki.ubuntu.com/DashAsBinSh
如果事实证明您实际上在使用 Dash,这里有一些关于差异的有用信息以及如何返回使用 Bash(如果您愿意):https: //wiki.ubuntu.com/DashAsBinSh
回答by Max Spencer
$ foo=foobarfoobar
$ echo ${foo}/bar/baz
foobarfoobar/bar/baz
Just that you have the braces in the wrong place, but then I am no expert at BASH, so perhaps this isn't the effect you're going for..
只是你把大括号放在错误的地方,但我不是 BASH 的专家,所以也许这不是你想要的效果..