我想在字符串中嵌入单引号 [英] I want to embed a single quote in a string
问题描述
通常,为了在字符串中插入引号,请使用 \
(反斜杠).
Usually, in order to embed a quotation mark in a string, \
(backslash) is used.
Bash脚本中的反斜杠含义不同吗?
Does a backslash have a different meaning in a Bash script?
我下面的代码不起作用:不包括引号,并且报告了以下错误:
My code below isn't working: the quotation mark wasn't included, and the following errors were reported:
recursive_merge.sh: line 7: unexpected EOF while looking for matching `''
recursive_merge.sh: line 14: syntax error: unexpected end of file
我没有解释.第14行甚至不存在.
I have no explanation. Line 14 doesn't even exist.
#!/bin/bash
#############this file should be in the directory directly upper than p0x. sphnum.txt should also be at the same directory
for i in 02 03 04 05 06 07 09 10 11 12 13 14 15 16 17 20 21 22 23 24 25; do
x=$(grep $i sphnum.txt |cut -c5-6)
y=$(grep $i sphnum.txt |cut -c8-9)
z=$(echo '\''$i'.ala.r'$x'.sph '$i'.ala.r'$y'.sph\'')
w=$(echo $i'.ala.r'$x'r'$y'.sph')
echo $z
echo $w
cd p$i/spheres.10_2_75/sph/
/project/Biogroup/Software/Docking/MergeSpheres.pl -s $z -o $w -n 500 &
cd ../../../
done
推荐答案
如 tripleee 指出的那样关于这个问题,在这种特定情况下,最佳方法是使用双引号字符串,在其中可以嵌入两个变量引用(例如, $ i
)和单引号按原样" ;例如: z ='$ i.ala.r $ x.sph $ i .ala.r $ y.sph'"
此答案集中于生成/嵌入文字'
字符的各种方法.字符串,从OP的误解开始.
As tripleee points out in comments on the question, the best approach in this particular scenario is to use a double-quoted string, in which you can embed both variable references (e.g., $i
) and single quotes as-is; e.g.: z="'$i.ala.r$x.sph $i .ala.r$y.sph'"
This answer focuses on the various approaches to producing / embedding literal '
chars. in strings, starting with the OP's misconception.
您对'\''
的使用表明您对通常用于将整体单引号字符串中的单引号嵌入"的变通方法感到困惑.>,这不是您的代码在 z = ...
行上执行的操作,因为它以'\''开始
.
Your use of '\''
suggests that you're confused by the workaround that is commonly used to "embed" a single quote in an overall single-quoted string, which is not what your code does on the z=...
line, because it starts with '\''
.
如果我们简化您的命令,则会得到:
If we simplify your command, we get:
echo '\''$i
这是一个语法错误,因为对于Bash来说,单引号是不平衡,因为'\'
本身是被视为包含文字 \
的完整单引号字符串,后跟 second 的 opening '
单引号字符串,永远不会闭合.
which is a syntax error, because to Bash the single quotes are unbalanced, because '\'
by itself is considered a complete single-quoted string containing literal \
, followed by the opening '
of a second single-quoted string, which is never closed.
再次值得注意的是,'$ i"
是解决此特定问题的最佳解决方案:'
可以原样嵌入,包括用双引号引起来的变量引用 $ i
可以保护其值免受潜在有害的
Again it's worth noting that "'$i"
is the best solution to this specific problem: the '
can be embedded as-is, and including variable reference $i
inside the double-quoted string protects its value from potentially unwanted word-splitting and filename expansion (globbing).
类似POSIX的外壳程序无法将单引号嵌入单引号内的字符串-甚至无法转义.因此,'\'
中的 \
仅被视为文字(参见以下解决方法).
POSIX-like shells provide NO way to embed single quotes inside a single-quoted string - not even with escaping. Hence, the \
in '\'
is simply treated as a literal (see below for a workaround).
此答案的其余部分显示所有生成带字面的'
的方法,包括用引号括起来的字符串的内部和外部.
The rest of this answer shows all approaches to producing a literal '
, both inside and outside quoted strings.
要在带引号的字符串 外 中创建单引号,只需使用 \'
:
To create a single quotation mark outside of a quoted string, simply use \'
:
$ echo I am 6\' tall.
I am 6' tall.
这仅使用 \
引用(转义)各个'
字符.但是请注意,放置在命令行中单引号或双引号字符串的上下文之外的标记应受
This quotes (escapes) the individual '
character only, using \
.
But note that tokens placed outside the context of a single- or double-quoted string on a command line are subject to word-splitting and filename expansion (globbing).
要在双引号字符串中使用单引号,请按原样使用(无需转义):
To use a single quote inside a double-quoted string, use it as-is (no escaping needed):
$ echo "I am 6' tall."
I am 6' tall.
如果您还希望嵌入变量引用(例如, $ i
)或命令(通过命令替换, $(...)
),这是最佳选择.在您的字符串中(您可以通过将 $
转义为 \ $
来抑制插值).
This is the best choice if you also want to embed variable references (e.g., $i
) or commands (via command substitutions, $(...)
) in your string (you can suppress interpolation by escaping $
as \$
).
要在单引号字符串中使用单引号(其中, no 插值(扩展)是按设计执行的),必须使用解决方法:
To use a single quote inside a single-quoted string (in which no interpolations (expansions) are performed by design), you must use a workaround:
$ echo 'I am 6'\'' tall.'
I am 6' tall.
要解决此问题,是因为单引号的字符串根本不支持嵌入的单引号 ;'\''
部分仅在用单引号括起来的字符串内"有意义:
The workaround is necessitated by single-quoted strings not supporting embedded single quotes at all; the '\''
part only makes sense "inside" a single-quoted string in that:
- 前导
'
终止到目前为止用单引号引起来的字符串 然后, -
\'
会生成一个'
文字,该文字用\
outside 引号字符串的上下文分别转义 - 结尾的
'
然后重新启动"单引号字符串的其余部分.
- the leading
'
terminates the single-quoted string so far - the
\'
then produces a'
literal individually escaped with\
outside the context of a quoted string. - the trailing
'
then "restarts" the remainder of the single-quoted string.
换句话说:虽然您不能直接嵌入单引号,但是可以将单引号的字符串分成多段,分别插入 \
-escaped '
实例 outside 根据需要将单引号字符串包括在内,然后让Bash的字符串串联(自动将直接相邻的字符串连接在一起)将它们全部拼凑在一起以形成单个字符串.
In other words: While you cannot directly embed a single quote, you can break the single-quoted string into multiple pieces, insert individually \
-escaped '
instances outside the single-quoted string as needed, and let Bash's string concatenation (which automatically joins directly adjacent string) piece it all back together to form a single string.
chepner 在注释中指出,您可以替代地使用
chepner points out in a comment that you can alternatively use a here-document with a quoted opening delimiter, which acts like a single-quoted string while allowing embedding of '
chars:
read -r var <<'EOF' # quoted delimiter -> like a '...' string, but ' can be embedded
I am 6' tall.
EOF
使用 未加引号的开头定界符,此处文档的作用类似于双引号字符串,就像后者一样,允许嵌入'
,同时还支持扩展:
With an unquoted opening delimiter, the here-document acts like a double-quoted string, which, just like the latter, also allows embedding '
, while also supporting expansions:
read -r var <<EOF # unquoted delimiter -> like a "..." string
$USER is 6' tall.
EOF
Finally, if remaining POSIX-compliant is not a must, you can use an ANSI C-quoted string string, which allows embedding single quotes with \'
;
note that such strings interpret control-character escape sequences such as \n
, but otherwise, like a normal single-quoted string, do not perform interpolation of variable references or command substitutions:
$ echo $'I am 6\' tall.'
I am 6' tall.
这篇关于我想在字符串中嵌入单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!