A metacharacter is any character that has a special meaning, such as a carat (^), a dollar sign ($), or an asterisk . Linux has a fair number of these metacharacters, and their meanings differ depending on which Linux command

you are using.

Full Stop as Metacharacter (.) Full Stop ( . ) Indicates the current position when running commands such as cd , find or sh. In applications such as awk , grep and sed

is a wildcard that represents a specific number of any character. For example, the following command finds everything MP3 files

находить . -тип f -name '* .mp3'

in the current folder and its subfolders.

If you run this command in the current working directory (pwd), you will see the returned results, assuming you store the MP3 files in the music folder in your home folder.

ps -ef | grep f..efox

Now look at this command: The ps command lists all running processes on your computer.

The grep command takes lines of input and looks for a pattern.

linux dot The ps -ef command returns a list of running processes. metacharacter pipe( | ) sends this list to the grep command, which looks for any line in the list containing f..efox

where dots refer to any two characters. If Firefox is running, you will get a match.

Likewise, if a program named fonefox or freefox is running, these are also returned. If you only need to look for a single character, instead of a dot (.) metacharacter, use ? metacharacter. Using ?

A metacharacter refers to any single character either at the beginning or at the end of a pattern.

Asterisk as a metacharacter Asterisk ( * ) is a well-known metacharacter.

ls * .flac
This means zero or more of any character when searching for a pattern.

List of asterisks in Linux List of asterisks in Linux Part *.flac

ps -ef | grep f * efox

command returns a match for any filename ending in .flac.

Likewise, you can use an asterisk with the grep command in the last section as follows:

Asterisk symbol in Linux

Asterisk symbol in Linux Carat as a wildcard (^) Carat ( ^

) is used to mark the beginning of a line or string.

ls -a

So how is it used? The ls command lists the files in a folder like this:

ls -a | grep ^ gnome

If you want to list files in a folder that starts with a specific string, like gnome, carat can be used to specify that string. For example:

This lists files that start with gnome.

character in carats character in carats

In the example above, the ls command returns a list of filenames and passes this list to the grep command, which is used for pattern matching.

Grep knows that the character karat means find anything that starts with the characters following it, and in this case it is gnome. Dollar symbol as metacharacter ($) Dollar symbol ( $ ) has multiple meanings as a metacharacter in Linux.

ls | grep png $

When used for pattern matching, it means the opposite of carat and denotes any pattern that ends with a specific string.

For example: This list contains all files ending with png.

#! / bin / bash 
export dog = molly
echo $ dog
The dollar symbol is also used to access environment variables in the bash shell.

Linux dollar sign Linux dollar sign Line export dog = molly creates an environment variable named dog and sets its value molly . To access an environment variable, use the $ symbol. With symbol $ operator echo $dog displays Molly. Without this in expression

echo dog

the word dog is displayed. Escaping metacharacters (\\) Sometimes you don’t want a metacharacter to have a special meaning. For example, if the file is named f.refox, and the other file is called


ls | grep f.refox

. Now look at the following command:

What do you think is coming back?

Linux escape character

ls | grep f \\. refox

Linux escape character[]To return just f.refox, escape the full stop to actually mean the full stop like this:

Parentheses as a metacharacter ( [] ) You can use brackets ( ) when looking for patterns.

ls | grep [abc]

Square brackets indicate specific letters to match anywhere in the pattern.

For example:

Linux brace character

Linux brace character You can use square brackets at the beginning, end, or middle of a search pattern. Use a hyphen (-) to search for a range of letters. [ah] For example, ls


returns files that start with any letter from a to h.

Grave Accent Metacharacter In the examples above, the pipe metacharacter sends the results of one command (such as the ls command) to another command (such as the grep command). An alternative way to do this is to use a back quote, also known as an accent grave ( ` ) to paste the results of one command into another command.

command = `ls -lah` 
echo $ command
To do this, store the result of one command in a variable.

Backslash character in Linux Backslash character in Linux The example is very contrived and you will probably never do anything like this, but you can see how it works.

The backstrike symbol starts the command and saves the result.

This can be useful in scripting, but is less flexible than using a pipe for simple commands. Common metacharacters and their meanings
symbol Meaning
, Any character.
* Zero or more characters.
^ Matches any line or line that starts with a pattern (e.g. ^gnome).
$ Match any string or string that ends with a pattern (e.g. gnome $).
[] \ [«abc», «def»] Escapes the next character to remove its special meaning [1..9]Matches one of the list or range (for example,
or ).
+ Match one or more of the previous ones (e.g. grep +).

Похожие записи