使用球替换变量
阅读了来自bash
我正在努力替换一个变量的一部分:
只是我用来了解更多
myvar="value1=aaa value2=bbb value3=ccc value4=ddd"
value2="zzz"
myvar="${myvar//value2=*([a-z0-9-])/value2=${value2}}"
结果的示例:
echo $myvar
value1=aaa value2=zzz
预期结果:
echo $myvar
value1=aaa value2=zzz value3=ccc value4=ddd
上面代码的结果不是预期的,因为它删除了Value2 =的所有内容Myvar的BBB。
如果我使用:
shopt -s extglob
它正在使用Linux的虚拟机来工作,但是即使脚本的启动始于#!/bin/bash,也不是从我的当前机器中使用。很可能是因为我不在终端中使用bash。
还有其他方法可以实现这一目标吗? (例如,使用SED不启用ExtGlob)
THX
After reading some docs from globs in bash
https://mywiki.wooledge.org/glob#extglob
I´m struggling to replace a part of a variable:
Just an example that i am using to learn a bit more
myvar="value1=aaa value2=bbb value3=ccc value4=ddd"
value2="zzz"
myvar="${myvar//value2=*([a-z0-9-])/value2=${value2}}"
result:
echo $myvar
value1=aaa value2=zzz
expected result:
echo $myvar
value1=aaa value2=zzz value3=ccc value4=ddd
The result of the code above is not the expected one as it´s deleting everything behind value2=bbb in myvar.
If I use:
shopt -s extglob
it is working from a virtual machine with linux but from my current machine is not even if the begining of the script starts with #!/bin/bash. Most probably because i´m not using bash in my terminal.
Any other way to achieve this? (with sed for example without enabling extglob)
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您真的不能使用bash,那么您可以使用最低的公共分母外壳功能来完成您想做的事情:
If you really can't use Bash, then you can maybe do what you want with lowest common denominator shell features like this: