string Shell 脚本 - 从变量中删除第一个和最后一个引号 (")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9733338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script - remove first and last quote (") from a variable
提问by user1263746
Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient way?
下面是来自一个较大脚本的 shell 脚本的片段。它从变量保存的字符串中删除引号。我是用 sed 做的,但它有效率吗?如果没有,那么有效的方法是什么?
#!/bin/sh
opt="\"html\test\\""
temp=`echo $opt | sed 's/.\(.*\)//' | sed 's/\(.*\).//'`
echo $temp
回答by wieczorek1990
回答by huelbois
There's a simpler and more efficient way, using the native shell prefix/suffix removal feature:
有一种更简单、更有效的方法,使用原生 shell 前缀/后缀删除功能:
temp="${opt%\"}"
temp="${temp#\"}"
echo "$temp"
${opt%\"}
will remove the suffix "
(escaped with a backslash to prevent shell interpretation).
${opt%\"}
将删除后缀"
(用反斜杠转义以防止 shell 解释)。
${temp#\"}
will remove the prefix "
(escaped with a backslash to prevent shell interpretation).
${temp#\"}
将删除前缀"
(用反斜杠转义以防止 shell 解释)。
Another advantage is that it will remove surrounding quotes only if there are surrounding quotes.
另一个优点是只有当有周围的引号时它才会删除周围的引号。
BTW, your solution always removes the first and last character, whatever they may be (of course, I'm sure you know your data, but it's always better to be sure of what you're removing).
顺便说一句,您的解决方案总是删除第一个和最后一个字符,无论它们是什么(当然,我确定您知道您的数据,但最好确定要删除的内容)。
Using sed:
使用 sed:
echo "$opt" | sed -e 's/^"//' -e 's/"$//'
(Improved version, as indicated by jfgagne, getting rid of echo)
(改进版本,如 jfgagne 所示,摆脱了回声)
sed -e 's/^"//' -e 's/"$//' <<<"$opt"
So it replaces a leading "
with nothing, and a trailing "
with nothing too. In the same invocation (there isn't any need to pipe and start another sed. Using -e
you can have multiple text processing).
所以它用空代替了前导"
,也用空代替了尾随"
。在同一个调用中(不需要管道并启动另一个 sed。使用-e
您可以进行多个文本处理)。
回答by Steven Penny
This will remove all double quotes.
这将删除所有双引号。
echo "${opt//\"}"
回答by user1587520
回答by Some programmer dude
You can do it with only one call to sed
:
您只需调用一次即可sed
:
$ echo "\"html\test\\"" | sed 's/^"\(.*\)"$//'
html\test\
回答by Dr.Kameleon
The shortest way around- try:
最短的方法- 尝试:
echo $opt | sed "s/\"//g"
It actually removes all "
s (double quotes) from opt
(are there really going to be any more double quotes other than in the beginning and the end though? So it's actually the same thing, and much more brief ;-))
它实际上删除了所有的"
s(双引号)opt
(除了开头和结尾之外,真的会有更多的双引号吗?所以它实际上是一样的,而且更简短;-))
回答by killjoy
If you're using jq and trying to remove the quotes from the result, the other answers will work, but there's a better way. By using the -r
option, you can output the result with no quotes.
如果您使用 jq 并尝试从结果中删除引号,则其他答案将起作用,但有更好的方法。通过使用该-r
选项,您可以输出不带引号的结果。
$ echo '{"foo": "bar"}' | jq '.foo'
"bar"
$ echo '{"foo": "bar"}' | jq -r '.foo'
bar
回答by Hiro.Protagonist
The easiest solution in Bash:
Bash 中最简单的解决方案:
$ s='"abc"'
$ echo $s
"abc"
$ echo "${s:1:-1}"
abc
This is called substring expansion (see Gnu Bash Manualand search for ${parameter:offset:length}
). In this example it takes the substring from s
starting at position 1 and ending at the second last position. This is due to the fact that if length
is a negative value it is interpreted as a backwards running offset from the end of parameter
.
这称为子字符串扩展(请参阅Gnu Bash 手册并搜索${parameter:offset:length}
)。在这个例子中,它从s
位置 1 开始到倒数第二个位置结束子字符串。这是因为 iflength
是一个负值,它被解释为从parameter
.
回答by Jonathan Lin
Update
更新
A simple and elegant answer from Stripping single and double quotes in a string using bash / standard Linux commands only:
仅使用 bash/标准 Linux 命令在字符串中剥离单引号和双引号的简单而优雅的答案:
BAR=$(eval echo $BAR)
strips quotes from BAR
.
BAR=$(eval echo $BAR)
从BAR
.
=============================================================
================================================== ============
Based on hueybois's answer, I came up with this function after much trial and error:
根据 Hueybois 的回答,经过多次反复试验,我想出了这个函数:
function stripStartAndEndQuotes {
cmd="temp=${%\\"}"
eval echo $cmd
temp="${temp#\"}"
eval echo "=$temp"
}
If you don't want anything printed out, you can pipe the evals to /dev/null 2>&1
.
如果您不想打印任何内容,您可以将 evals 通过管道传输到/dev/null 2>&1
.
Usage:
用法:
$ BAR="FOO BAR"
$ echo BAR
"FOO BAR"
$ stripStartAndEndQuotes "BAR"
$ echo BAR
FOO BAR