下面是一个大脚本中的shell脚本片段。它从变量持有的字符串中删除引号。我正在使用sed,但它是有效的吗?如果不是,那么什么是有效的方法?

#!/bin/sh

opt="\"html\\test\\\""
temp=`echo $opt | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/'`
echo $temp

当前回答

STR='"0.0.0"' ## OR STR="\"0.0.0\""
echo "${STR//\"/}"
## Output: 0.0.0 

其他回答

使用tr删除“:

 echo "$opt" | tr -d '"'

注意:这并没有完全回答问题,删除了所有双引号,而不仅仅是开头和结尾。请看下面的其他答案。

如果您试图删除引号,因为Makefile保留了它们,请尝试以下方法:

$(subst $\",,$(YOUR_VARIABLE))

基于另一个答案:https://stackoverflow.com/a/10430975/10452175

你只需要调用一次sed就可以做到:

$ echo "\"html\\test\\\"" | sed 's/^"\(.*\)"$/\1/'
html\test\
Linux=`cat /etc/os-release | grep "ID" | head -1 | awk -F= '{ print $2 }'`

echo $Linux
Output:
"amzn"

从变量中删除双引号的最简单方法是

Linux=`echo "$Linux" | tr -d '"'` 
Linux=$(eval echo $Linux)
Linux=`echo ${Linux//\"/}`
Linux=`echo $Linux | xargs`

All提供不带双引号的输出:

echo $ Linux

amzn

STR='"0.0.0"' ## OR STR="\"0.0.0\""
echo "${STR//\"/}"
## Output: 0.0.0