What are metacharacters in Linux

The basic structure (according to widespread syntax) of a command should look like this:

Command name [options] [arguments ...]

Options are introduced with "-" and usually consist of just one letter. Several options can be grouped:

Any number of arguments can be passed to the command. Most of the time options and arguments can be exchanged in their order.

The leading minus to the introduction of options is, according to the Unix98 standard. Some commands (like ps, tar, ...) also use options after the BSD syntax, after which no leading minus is prescribed:

The GNU rules for long options, which are introduced by two leading minus signs.

user @ sun> ps --sort tty

 

Before a command is executed, bash interprets the command line passed to it. The shell parses the input and searches for characters (strings) with special meanings. Bash then tries to substitute these strings. The order in which the arguments are viewed plays a major role in understanding how Bash works.

In the section Shells, Bash the interested reader will find a detailed discussion on parsing the command line. The examples here are limited to the essentials, which should, however, already cover the everyday life of most Linux users.

Metacharacters are those special characters in a shell that are used as placeholders for character patterns.

In Bash, the following special characters stand for:

?

Any character exactly

*

Any number of characters (including 0)

[def]

One of the characters

[^ czx]

None of the characters specified

[! czx]

As above

[a-f]

All characters from the range

~

Stands for the home directory

\

Cancels the special meaning of the following character

"..."

Removes the special meaning of spaces and tabs from the enclosed string (the string is saved as a a Word considered)

'...'

Removes any special meanings from the enclosed string

A metacharacter is never interpreted by the respective command, but always by the shell. Because of this, a command like

user @ sun> cp * .x * .y

not under a Unix shell.

How Bash works can be illustrated using examples:

user @ sun> ls /boot/*.map
/boot/System.map

The asterisk "*" stands for any number of characters. This means that in the above entry, Bash replaces the entry with all file names that are in the / boot directory and end in ".map".

user @ sun> ls -d /? [abc] *
/ sbin / var

The bash replaces the input with all file names from the root directory, the first character of which is arbitrary (?), whose second letter is either an "a", a "b" or a "c", followed by any number (including 0) of any characters "*". The "-d" option of the "ls" command means that only their names are listed for directories, not their contents.

user @ sun> ls /? [abc] *
/ sbin:
SuSEconfig fsck.minix ldconfig rmmod
YaST genksyms lilo route ...

/ var:
X11R6 cron lock named spool texfonts
adm games log openwin squid tmp
...

... without the "-d" option, the output can quickly become confusing.

user @ sonne> echo "Welcome $ USER"
Welcome user
user @ sonne> echo 'Welcome $ USER'
Welcome $ USER

USER is a predefined shell variable that contains the user ID of the logged on user. If the argument of echo "quoted" with double quotes means that the space is ignored, but not that $-Character that shows the content of the variable here USER retrieves.
When enclosed in single quotation marks, the special meaning of each character is canceled, including the variable substitution it contains.