如果我的变量有数据,
test="score=5,grade=d,pass=f,"
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以从变量中提取数据/重写数据,
test="score,grade,pass"
Run Code Online (Sandbox Code Playgroud)
我不需要的数据=,&和,字符.
在纯bash中(不需要像sed或awk或perl这样的外部工具),可以使用"参数扩展"来操作字符串.您可以在Bash的手册页中阅读此内容.
#!/usr/bin/env bash
test="score=5,grade=d,pass=f,"
# Use the "extglob" shell option for "extended" notation on patterns.
shopt -s extglob
# First, remove the bits from equals to before each comma...
test="${test//=+([^,]),/,}"
# Next, remove the trailing comma.
test="${test%,}"
echo "$test"
Run Code Online (Sandbox Code Playgroud)
而且这里有一个演示.