简体   繁体   中英

List of characters which needs to be escaped in a linux shell command

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 :

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.

If someone may point to a resource where I can get an entire list of characters, it would be a great help.

Many Thanks

Quoting from Shell Command Language :

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.

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...

您可以使用单引号'filename'来转义所有需要在shell模式下转义的内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM