Shell operations and simple programming (4)

Source: Internet
Author: User
Tags echo command

Shell special characters

Wildcard

Wildcard characters are used for pattern matching, such as file name matching, path name searching, and string searching. Common wildcard characters include (*),(?) And the Character Sequence enclosed in square brackets. You can include These wildcards in the file name as a command parameter to form a so-called "pattern string" for pattern matching during execution.

* Represents any string (length can be different). For example, f * matches any string starting with F. Note that the dot (.) before the file name must be explicitly matched with the slash (/) in the path name. For example, "*" cannot match. file, but ". *" can match. File.

? Represents any single character.

[] Indicates a specified character range. As long as the character at the [] position in the file name is within the range specified by [], the file name matches the pattern string. The character range in square brackets can be composed of directly given characters, and can also be composed of starting characters, ending characters, and intermediate hyphens (-) that indicate a limited range. For example, F [a-d] and f [ABCD] serve the same purpose. Shell uses all file names that match the pattern string specified in the command line as command parameters to form the final command and then execute the command.

Table 3-1 describes the meanings of these wildcards.

Table 3-1

Modular string

Meaning

*

Name of all documents in the current directory

* Text *

Names of all files with text in the current directory

[AB-DM] *

Names of all files starting with A, B, C, D, and m in the current directory

[AB-DM]?

The names of all files starting with a, B, c, d, M and followed by only one character in the current directory

/Usr/bin /??

The names of all files with two characters under the directory/usr/bin

Note that the hyphen (-) is only valid in square brackets and indicates the character range. For example, it becomes a common character outside square brackets. "*" And "?" Wildcards are used only outside square brackets. If they appear in square brackets, they also lose the wildcard capability and become common characters. For example, mode-A [*?] In ABC, only one square brackets are wildcard characters, "*" and "?". Are common characters. Therefore, the matching strings can only be-a * ABC and-? ABC.

Finally, we will explain some issues that need to be paid attention to when using wildcards. Because "*", "?" And [] are of special significance for shell, so these characters should not appear in normal file names. In particular, do not show them in the directory name; otherwise, shell matching may be infinitely recursive. In addition, if the directory does not contain a file name that matches the specified mode string, shell will use this mode string as a parameter to send it to related commands. This may be the reason for the special characters in the command.

  Quotation marks

In shell, there are three types of quotation marks: single quotation marks, double quotation marks, and reverse quotation marks.

1. Single quotes

All characters enclosed by single quotes are common characters. When special characters are enclosed in single quotes, they will also lose their original meaning and will only be interpreted as common characters. For example:

# String = '$ Path'

# Echo $ string

# Path

It can be seen that $ retains its meaning and appears as a common character.

2. Double quotation marks

Characters enclosed by double quotation marks. Except for $,/, 'and ", these characters are still special characters and keep their special functions. Other characters are still treated as common characters. For $, the variable value specified later is used to replace this variable and $; For/, it is an escape character, it tells shell not to perform special processing on the character after it, just as a common character. You can imagine that the double quotation marks must contain only four characters, $,/, 'and. For the "number, if no/is added before it, shell will match it with the previous" number.

For example, assume that the path value is.:/usr/bin:/bin and enter the following command:

# String = "$ PATH //"/$ path"

# Echo $ teststring

.:/Usr/bin:/bin/"$ path

You can try it by yourself without adding/before the second double quotation mark.

3. Reverse quotation marks

The key corresponding to the character ') is generally located in the upper left corner of the keyboard. Do not confuse it with the single quotation mark. Shell interprets the string enclosed by backquotes as a command line. During execution, shell first executes the command line, and replaces the entire anti-quotation mark (including two anti-quotation marks) with its standard output result. For example:

# Pwd

/Home/XYZ

# String = "Current directory is 'pwd '"

# Echo $ string

Current directour is/home/XYZ

When shell executes the echo command, it first executes the command PWD in 'pwd', replaces the output result/home/XYZ with the 'pwd', and finally outputs the entire replaced result.

This function can be used to replace commands. That is, the execution result enclosed by backquotes is assigned to the specified variable. For example:

# Today = 'date'

# Echo today is $ today

Today is Mon Apr 15 16:20:13 CST 1999

Backquotes can also be nested. However, you must note that the inner anti-quotation marks must be escaped using a backslash (/) When nesting. For example:

# Abc = 'echo the number of users is/'who | WC-L /''

# Echo $ ABC

The number of users is 5

  Annotator

In shell programming or Linux configuration documents, you often need to comment on some body lines to increase the readability of the program. In shell, the text line starting with character # represents the comment line.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.