Bash - Quoting

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

Quoting is used to remove the special meaning of certain :

Quoting can be used to:

Each of the metacharacters has special meaning to the shell and must be quoted if it is to represent itself.

When the command history expansion facilities are being used, the history expansion character, usually !, must be quoted to prevent history expansion.

Advertising

3 - Quoting Mechanisms

There are three quoting mechanisms:

3.1 - Escape Character

3.2 - Single Quote

Enclosing characters in single quotes preserves the literal value of each character within the quotes.

A single quote may not occur between single quotes, even when preceded by a backslash.

3.3 - Double Quote

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, , \, and, when history expansion is enabled, !.

The characters $ and retain their special meaning within double quotes.

The backslash retains its special meaning only when followed by one of the following characters:

  • $,
  • ,
  • ,
  • \,
  • or <newline>.

A double quote may be quoted within double quotes by preceding it with a backslash.

If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

The special parameters * and @ have special meaning when in double quotes. See parameters.

A double-quoted string preceded by a dollar sign ($) will cause the string to be translated according to the current locale. If the cur- rent locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

Advertising

4 - Documentation / Reference

  • comes form the man page of bash
man bash>