Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

bash

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

like image 937
Arunkumar Avatar asked Dec 15 '22 05:12

Arunkumar


1 Answers

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

like image 122
devnull Avatar answered May 10 '23 23:05

devnull