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

#!/bin/sh

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

当前回答

这是不使用sed的最离散的方式:

x='"fish"'
printf "   quotes: %s\nno quotes:  %s\n" "$x" "${x//\"/}"

Or

echo $x
echo ${x//\"/}

输出:

   quotes: "fish"
no quotes:  fish

我从线人那里得到的。

其他回答

这是不使用sed的最离散的方式:

x='"fish"'
printf "   quotes: %s\nno quotes:  %s\n" "$x" "${x//\"/}"

Or

echo $x
echo ${x//\"/}

输出:

   quotes: "fish"
no quotes:  fish

我从线人那里得到的。

如果您正在使用jq并试图从结果中删除引号,那么其他答案也可以工作,但还有更好的方法。通过使用-r选项,可以输出不带引号的结果。

$ echo '{"foo": "bar"}' | jq '.foo'
"bar"

$ echo '{"foo": "bar"}' | jq -r '.foo'
bar

还有另一种方法。如:

echo ${opt:1:-1}
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,试试这个。——输出文本