下面是一个大脚本中的shell脚本片段。它从变量持有的字符串中删除引号。我正在使用sed,但它是有效的吗?如果不是,那么什么是有效的方法?
#!/bin/sh
opt="\"html\\test\\\""
temp=`echo $opt | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/'`
echo $temp
下面是一个大脚本中的shell脚本片段。它从变量持有的字符串中删除引号。我正在使用sed,但它是有效的吗?如果不是,那么什么是有效的方法?
#!/bin/sh
opt="\"html\\test\\\""
temp=`echo $opt | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/'`
echo $temp
当前回答
如果您正在使用jq并试图从结果中删除引号,那么其他答案也可以工作,但还有更好的方法。通过使用-r选项,可以输出不带引号的结果。
$ echo '{"foo": "bar"}' | jq '.foo'
"bar"
$ echo '{"foo": "bar"}' | jq -r '.foo'
bar
其他回答
如果您试图删除引号,因为Makefile保留了它们,请尝试以下方法:
$(subst $\",,$(YOUR_VARIABLE))
基于另一个答案:https://stackoverflow.com/a/10430975/10452175
你只需要调用一次sed就可以做到:
$ echo "\"html\\test\\\"" | sed 's/^"\(.*\)"$/\1/'
html\test\
Bash中最简单的解决方案:
$ s='"abc"'
$ echo $s
"abc"
$ echo "${s:1:-1}"
abc
这被称为子字符串展开(参见Gnu Bash手册并搜索${parameter:offset:length})。在这个例子中,它从s获取子字符串,从位置1开始,到最后第二个位置结束。这是因为,如果length为负值,则它将被解释为从参数末尾开始的向后运行偏移。
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
如果你来这里是为了aws cli -query,试试这个。——输出文本