且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除变量开头和结尾的子字符串匹配模式

更新时间:2021-08-15 23:26:06

好吧,您不能嵌套${var%}/${var#}操作,因此必须使用临时变量.

Well, you can't nest ${var%}/${var#} operations, so you'll have to use temporary variable.

就像这里:

var="http://whatever/score/"
temp_var="${var#http://}"
echo "${temp_var%/score/}"

或者,您可以将正则表达式与(例如)sed一起使用:

Alternatively, you can use regular expressions with (for example) sed:

some_variable="$( echo "$var" | sed -e 's#^http://##; s#/score/$##' )"