bash - 如何使用正则表达式(bash)为变量赋值?

标签 bash

我想在变量赋值行的 bash 中使用正则表达式

例如

oldip="14\.130\.31\.172"
oldip_a="14.130.31.172" //How to use regex on this line.

如何使用正则表达式删除$oldip中的所有'\'?然后给 $oldip_a 赋新值。

你有什么想法吗?

最佳答案

我相信你想像这样使用字符串替换:

    oldip_a=${oldip//\\/}

或者类似的东西......当然总是有一些逃避反斜杠的战斗!

一个更明显的例子:

    some_variable=${some_other_variable//replaceEachOfThese/withThis}

在此页面上搜索“替换所有匹配项”:

http://tldp.org/LDP/abs/html/string-manipulation.html

关于bash - 如何使用正则表达式(bash)为变量赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696719/

相关文章:

bash - 从 fasta 文件打印序列

r - 在 R : broken pipe error 中传递两个 bash 命令

linux - 识别并删除所有 x64 架构文件

linux - BASH:监听用户输入而不等待其他进程

regex - Bash 脚本 - 需要帮助才能使匹配和替换正常工作

bash - 从 Bash 脚本中的变量调用带空格的命令行

Bash while read line 循环不打印条件中的每一行

linux - 用于解析命令输出的 Bash 脚本

bash - 如何隐藏显示终端命令输出

bash - 从 bash 脚本运行 mongo 命令