special characters in shell

   In addition to ordinary characters, some special characters with special meanings and functions can also be used in the shell. Pay attention to their special meaning and scope when using them.
These special characters are described below.
1. Wildcards
  Wildcards are used for pattern matching, such as file name matching, path name search, string search, etc. Commonly used wildcards are *, ?, and character sequences enclosed in square brackets [ ]. Users can include these wildcards in filenames as command parameters to form a so-called "pattern string" that is pattern matched during execution.
  * stands for any string (of varying lengths), for example: "f*" matches any string starting with f. It should be noted, however, that the dot (.) before the filename and the slash (/) in the pathname must match explicitly. For example, "*" cannot match .file, while ".*" can match .file.
  ? represents any single character.
 [] represents a specified range of characters. As long as the character at the position of [] in the file name is within the range specified in [], the file name will match this pattern string. The character range in square brackets can consist of the characters given directly, or it can consist of the starting character, the ending character, and the middle hyphen (-) that denote a limited range. For example, f[a-d] does the same thing as f[abcd]. The shell will take all filenames that match the pattern specified on the command line as arguments to the command, form the final command, and then execute the command.
  Below we give a description of the specific meaning of these wildcards.
  *Text*
  The names of all files in the current directory that contain Text in their names.
  [ab-dm]*
  The names of all files starting with a, b, c, d, m in the current directory.
  [ab-dm]?
  The names of all files in the current directory that start with a, b, c, d, and m followed by only one character.

  The names of all two-character files in the   /usr/bin/?? directory /usr/bin.
  It should be noted that the hyphen "-" is only valid within square brackets, indicating a range of characters, such as outside the square brackets, it becomes a normal character. And * and ? are only wildcards outside the square brackets. If they appear inside the square brackets, they also lose the ability of wildcards and become ordinary characters. For example, only one pair of square brackets in the pattern "-a[*?]abc" is a wildcard, and * and ? are normal characters, so it can only match strings of -a*abc and -a?abc.
  Finally, some issues that need to be paid attention to when using wildcards are explained. Because *, ?, and [] have special meaning to the shell, these characters should not appear in normal file names. In particular, do not appear in directory names, otherwise the shell may recurse endlessly in matching. Another thing to note is that if there is no filename in the directory that matches the specified pattern, the shell will use the pattern itself as an argument to the command. This may be the reason for the special characters in the command.
2. Quotes There are three types of quotes
  in the shell: single quotes, double quotes and backticks.
  1) Single quotation marks '
  The characters enclosed by single quotation marks appear as ordinary characters. After the special characters are enclosed in single quotation marks, they will lose their original meaning and will only be interpreted as ordinary characters. For example:
  $ string='$PATH '
  $ echo $string
  $PATH
  $visible $ retains its meaning and appears as a normal character.
  2) Double quotation marks "
The characters enclosed by double quotation marks are still treated as ordinary characters except $, ', and ", which are still special characters and retain their special functions. For $, the following characters are specified. The value of the variable replaces this variable and $; for the escape character, it tells the shell not to perform special processing on the character that follows it, and only treat it as a normal character. It is conceivable that in double quotes, you need to add the preceding There are only four characters $, ' and " by themselves.
  For example, we assume the value of PATH is .:/usr/bin:/bin and enter the following command:
  $ TestString="$PATH"$PATH"
  $ echo $TestString
  .:/usr/bin:/bin"$PATH
  $reader You can try for yourself what will happen if you don't add the second double quote.
  3) Backtick `
  Backtick (`) The key corresponding to this character is generally located in the upper left corner of the keyboard, do not confuse it with the single quotation mark ('). A string enclosed in backticks is interpreted by the shell as a command line. When executed, the shell executes the command line first and replaces the entire backticks (including the two backticks) with its standard output. For example:
  $ pwd
  /home/xyz
  $ string=”current directory is `pwd`”
  $ echo $string
  current directour is /home/xyz
  $
  When the shell executes the echo command, first execute the command pwd in `pwd`, and output The result /home/xyz replaces the `pwd` part, and finally outputs the entire result after the replacement.
  Backticks can also be nested. Note, however, that inner backticks must be escaped with a backslash () when nested. For example:
  $ abc=`echo The number of users is `who| wc-l“
  $ echo $abc
  The number of users is 5
  $ Shell special characters can also be used on the command line between backticks. "In the result of the command, it is actually going to execute the command specified in ". When executing, the special characters in the command, such as $, ", ?, etc., will have special meanings, and " can contain any legal Shell commands, such as:
  $ ls
  note readme.txt Notice Unix.dir
  $ TestString="`echo $HOME ` ` ls [nN]*`"
  $ echo $TestString
  /home/yxz note Notice
  
3. Comments
  in shell programming It is often necessary to comment some text lines in order to increase the readability of the program. In Shell, the text lines starting with the character "#" represent comment lines.
4. Special characters used in data or program control:
   mainly:
   > (file) redirect output to file
   >> (file) redirect output to file, append content to the end of existing file
   < (file) redirect input to file
   ; command separator
   | Pipe character, redirect the output of one command to the input of another command
   & put it after the command to force the command to perform
   `` command substitution in the background, redirect the output of one command to the parameter of another command
5. Used for quoting and Escaped special characters:
1) The escape character \ indicates that the following characters have no special meaning or are not functions of the Shell,
2) In addition, Bash can also recognize some escape sequences in the C language, such as:
          \a sound ling
          \b
          Escape\e Escape
          \n Linefeed
          \r Enter
          \t Tab
          \v Tab
          \\ Backslash
          \nnn Octal ASCII code
          \xnnn Hexadecimal ASCII code
3) Quote characters ' and " : Quoting special characters or words separated by whitespace to form a simple string
The difference between the two is and, the content in double quotes can be used for parameter and variable substitution

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394636&siteId=291194637