bash 需要在 linux shell 命令中转义的字符列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19177076/
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
List of characters which needs to be escaped in a linux shell command
提问by Arunkumar
In Linux and other OS, file can contain characters like (,),[,],<space>,
etc. in their names.
Whenever I try to use any of these files in my bash command like cat, ls, etc. I am required to escape them like below :
在 Linux 和其他操作系统中,文件(,),[,],<space>,
的名称中可以包含诸如等字符。每当我尝试在我的 bash 命令中使用任何这些文件时,如 cat、ls 等。我需要像下面这样对它们进行转义:
filename abc(10-oct).txt
cat abc(10-oct).txt wont work.
If I precede "("
and ")"
characters with "\"
character like
如果我在字符之前"("
和字符像")"
"\"
cat abc\(10-oct\).txt
This works
这有效
I am trying to automate some of Linux shell commands via Java program.And I am not sure of what all characters I must take care of and escape them.
我正在尝试通过 Java 程序自动化一些 Linux shell 命令。我不确定我必须处理和转义所有字符。
If someone may point to a resource where I can get an entire list of characters, it would be a great help.
如果有人可以指出我可以获得完整字符列表的资源,那将是一个很大的帮助。
Many Thanks
非常感谢
回答by devnull
Quoting from Shell Command Language:
引用Shell 命令语言:
The following characters must be quoted if they are to represent themselves:
| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>
and the following may need to be quoted under certain circumstances. That is, these characters may be special depending on conditions described elsewhere in this specification:
* ? [ # ~ = %
The various quoting mechanisms are the escape character, single-quotes and double-quotes.
如果要代表自己,必须引用以下字符:
| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>
在某些情况下可能需要引用以下内容。也就是说,这些字符可能是特殊的,具体取决于本规范其他地方描述的条件:
* ? [ # ~ = %
各种引用机制是转义字符、单引号和双引号。
It also says:
它还说:
Enclosing characters in single-quotes (' ') preserves the literal value of each character within the single-quotes. A single-quote cannot occur within single-quotes.
将字符括在单引号 (' ') 中会保留单引号内每个字符的字面值。单引号内不能出现单引号。
And:
和:
Enclosing characters in double-quotes (" ") preserves the literal value of all characters within the double-quotes, with the exception of the characters dollar-sign, backquote and backslash...
用双引号 (" ") 括起来的字符会保留双引号内所有字符的字面值,但美元符号、反引号和反斜杠字符除外...
回答by Saddam Abu Ghaida
You can use Single Quote 'filename'
which will escape everything needs to be escaped in shell mode
您可以使用单引号'filename'
,它将转义所有需要在 shell 模式下转义的内容