问题描述
在C#中,有一个逐字字符串,以便
string c =hello \t world; // hello world
string d = @hello \t world; // hello \t world
我是shell脚本的新手,shell中是否有类似的方法?
由于我有很多文件夹,名称如服装与配饰>服装>活动服装,我想知道是否有一个简单的方法来处理逃生没有写这么多的字符。
test.sh
director =Apparel& Accessories>服装> Activewear
#任何逃避空间的动作&> ???
hadoop fs -ls $ director
定义您的示例中的特定字符串, Apparel&配件>服装>活动服
,双引号或单引号将工作;
在 shell (任何POSIX-兼容的外壳),您如何将引用到变量与您定义一样重要。
要安全地将 引用到以前定义的变量中,而没有副作用,请将其包含在双引号中,例如$ directory
。
要定义 literal(逐字)字符串:
(相反,要使用嵌入式变量引用或定义变量>嵌入式命令替换或嵌入式算术表达式,请使用双重引号()。 p>
-
如果您的字符串包含 NO 单个引号:
-
使用单引号字符串,例如:
目录='Appare l&配件>服装> Activewear'
-
一个单引号的字符串不受shell 的任何解释,因此通常是定义文字 的最安全的选项。 >。请注意,字符串可以跨越多行;例如:
multiline ='line 1
/ pre>
line 2'
-
如果您的字符串 DOES包含单引号(例如,
我在这里。
),你想要一个解决方案,在所有 POSIX兼容的shell p>
- 将字符串分割成多个(单引号)部分,单引号字符 strong>:
注意:可惜的是,单引号字符串不能包含单引号,甚至没有转义。
directory ='我'在这里。
字符串分为单引号 I
,然后是文字'
(作为 unquoted 字符串转义为 \'
),然后在这里单引号 m。
。由于零件之间没有空格,所以结果是一个单个字符串,包含 I
之后的文字单引号。
替代方法:如果您不介意使用多行语句,则可以使用这里引用的文档,如底部所述。
-
如果您的字符串 DOES包含单引号(例如
我在这里。
),你想要一个解决方案,在bash
,ksh
和zsh
:
-
使用 ANSI-C引号:
directory = $'I\'m
-
注意:如您所见,ANSI-C引用允许将单引号转换为 \'
,但请注意附加含义:其他 \< char>
序列需要解释,太;例如, \\\
被解释为换行符 - 请参阅
@chepner的帽子提示,他指出,将 POSIX兼容的方式直接包含在一个字符串中使用单引号即可逐字使用 是将 读取-r
与使用引号打开分隔符( -r
选项确保字符串中的 \
字符被视为文字)。
#*任何*形式的引用,而不仅仅是单引号,在开放EOF将工作。
#注意,$ HOME将不会被扩展。
#(如果你没有引用开头的EOF,它会。)
读-r目录<'EOF'
我在这里$ HOME
EOF
- 请注意,这里的文档创建 stdin 在这种情况下,输入(
读取
)。因此,您不能使用此技术直接传递生成的字符串作为参数。
In C#, there is a verbatim string so that,
string c = "hello \t world"; // hello world
string d = @"hello \t world"; // hello \t world
I am new to shell script, is there a similar method in shell?
Because I have many folders with the name like "Apparel & Accessories > Clothing > Activewear", I want to know if there is a easy way to process the escape characters without write so many .
test.sh
director="Apparel & Accessories > Clothing > Activewear"
# any action to escape spaces, &, > ???
hadoop fs -ls $director
For definining the specific string in your example, Apparel & Accessories > Clothing > Activewear
, either double quotes or single quotes will work; referring to it later is a different story, however:
In the shell (any POSIX-compatible shell), how you refer to a variable is just as important as how you define it.
To safely refer to a previously defined variable without side-effects, enclose it in double quotes, e.g., "$directory"
.
To define [a variable as] a literal (verbatim) string:
(By contrast, to define a variable with embedded variable references or embedded command substitutions or embedded arithmetic expressions, use double quotes ("
).)
If your string contains NO single quotes:
Use a single-quoted string, e.g.:
directory='Apparel & Accessories > Clothing > Activewear'
A single-quoted string is not subject to any interpretation by the shell, so it's generally the safest option for defining a literal. Note that the string may span multiple lines; e.g.:
multiline='line 1
line 2'
If your string DOES contain single quotes (e.g.,
I'm here.
) and you want a solution that works in all POSIX-compatible shells:- Break the string into multiple (single-quoted) parts and splice in single-quote characters:
Note: Sadly, single-quoted strings cannot contain single quotes, not even with escaping.
directory='I'\''m here.'
The string is broken into into single-quoted I
, followed by literal '
(escaped as an unquoted string as \'
), followed by single-quoted m here.
. By virtue of having NO spaces between the parts, the result is a single string containing a literal single quote after I
.
Alternative: if you don't mind using a multiline statement, you can use a quoted here document, as described at the bottom.
If your string DOES contain single quotes (e.g.,
I'm here.
) and you want a solution that works inbash
,ksh
, andzsh
:Use ANSI-C quoting:
directory=$'I\'m here.'
Note: As you can see, ANSI-C quoting allows for escaping single quotes as \'
, but note the additional implications: other \<char>
sequences are subject to interpretation, too; e.g., \n
is interpreted as a newline character - see http://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting
Tip of the hat to @chepner, who points out that the POSIX-compatible way of directly including a single quote in a string to be used verbatim is to use read -r
with a here document using a quoted opening delimiter (the -r
option ensures that \
characters in the string are treated as literals).
# *Any* form of quoting, not just single quotes, on the opening EOF will work.
# Note that $HOME will by design NOT be expanded.
# (If you didn't quote the opening EOF, it would.)
read -r directory <<'EOF'
I'm here at $HOME
EOF
- Note that here documents create stdin input (which
read
reads in this case). Therefore, you cannot use this technique to directly pass the resulting string as an argument.
这篇关于shell中有没有方法来忽略转义字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!