In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are the special characters in the linux shell". In daily operation, I believe many people have doubts about what are the special characters in the linux shell. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "what are the special characters in the linux shell"! Next, please follow the small series to learn together!
The shell is the interface between the user and the Linux operating system. There are several shells in Linux, the default being Bash. This chapter describes how shells work, the types of shells, the general operation of shells, and the characteristics of Bash.
Special characters in shell
Shell in addition to the use of ordinary characters, you can also use some special characters with special meaning and function. When using them, attention should be paid to their special meaning and scope of action.
1. wildcard
Wildcards are used for pattern matching, such as file name matching, pathname search, string lookup, and so on. Common wildcards are *,? and a sequence of characters enclosed in square brackets [ ]. Users can include these wildcards in file names as command parameters to form a so-called "pattern string" for pattern matching during execution.
* Represents any string (of variable length), for example: "f*" matches any string starting with f. Note, however, that the dot (.) before the file name The slash (/) in and path names must match explicitly. For example,"*" does not match.file, whereas ".* "to match.file.
? Represents any single character.
[] represents a specified character range, and the filename matches the pattern string as long as the characters at [] are within the range specified in []. The character range in square brackets can consist of the characters given directly, or it can consist of a start character, a stop character, and a hyphen (-) in between to indicate the limiting range. For example, f [a- d] has the same effect as f [abcd]. Shell will take all file names matching the pattern string specified on the command line as arguments to the command, form the final command, and then execute the command.
Table 10-1 below illustrates what these wildcards mean.
Table 10-1 Examples of wildcard meanings
pattern string
significance
*
The names of all files in the current directory.
*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 begin with a, b, c, d, m and are followed by only one character.
/usr/bin/??
The names of all two-character files in the directory/usr/bin.
It is important to note that the hyphen "-" is valid only within square brackets, indicating a range of characters, such as outside square brackets becomes a normal character. And * and? Wildcards appear only outside square brackets. If they appear inside square brackets, they also lose their wildcard ability and become ordinary characters. For example, the pattern "- a[*?] Only one pair of square brackets in abc"are wildcards, * and? Both are ordinary characters, so the strings it matches can only be- a*abc and- a? abc。、
*** Explain some issues that need to be noted when using wildcards. Because of *? And [] have special meanings for shells, so they should not appear in normal file names. In particular, don't include them in directory names, otherwise Shell matching may recurse indefinitely. Another thing to note is that if there is no file name in the directory that matches the specified pattern string, the Shell will pass the pattern string itself as an argument to the command. This is probably why special characters appear in commands.
2. quotation marks
There are three types of quotes in the shell: single quotes, double quotes and backquotes.
* single quote '
Characters enclosed in single quotes appear as normal characters. Special characters enclosed in single quotes lose their original meaning and are interpreted only as ordinary characters. For example:
$ string='$PATH' $ echo $string $PATH $
So $retains its meaning and appears as an ordinary character.
* Double quotes "
Characters enclosed in double quotes are treated as ordinary characters except for $,', and "which remain special characters and retain their special functions. In the case of $, it replaces the variable and $with the value of the variable specified after it; in the case of $, it is an escape character that tells the shell not to treat the character that follows it specially, just as a normal character. As you can imagine, the only four characters that need to be prefixed in double quotes are $,,'and "itself. If the "sign is not preceded by a prefix, Shell matches it to the preceding" sign.
For example, we assume PATH has a value of.:/ usr/bin:/bin, enter the following command:
$ TestString="$PATH"$PATH" $ echo $TestString .:/ usr/bin:/ bin"$PATH $
The reader can see for himself what happens when you leave the second quotation mark blank.
* backquotes
The backquote (') character is usually located in the upper-left corner of the keyboard and should not be confused with the single quote ('). A string enclosed in backquotes is interpreted by the shell as a command line, and when executed, the shell executes the command line first, replacing the entire backquotes (including the two backquotes) 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 echo command, it first executes the command pwd in `pwd`, and replaces `pwd` with/home/xyz output result, *** output the whole replaced result.
This feature allows command substitution, i.e. assigning the execution result enclosed in backquotes to a specified variable. For example:
$ today=`date` $ echo Today is $today Today is Mon Apr 15 16:20:13 CST 1999 $
Back quotes can also be nested. Note, however, that the inner backquotes 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 backquotes. Shell to get the result of the command in ``, it actually has to execute the command specified in ``. Special characters in commands, such as $,",? And so on will have a special meaning, and `` can contain any legal Shell command, such as:
$ ls note readme.txt Notice Unix.dir $ TestString="`echo $HOME ` ` ls [nN]*`" $ echo $TestString /home/yxz note Notice $
In other cases, readers can try it themselves.
1. comment character
In shell programming, it is often necessary to comment certain lines of text to increase the readability of the program. Text lines that begin with the character "#" in the Shell represent comment lines.
There are also special characters such as: > and for input/output redirection and pipes|;&; Command execution operators && and for background commands|| and {} representing command groups are described in the following sections.
Standard input/output and redirection
1. Standard input and output
We know that executing a shell command line usually automatically opens three standard files: the standard input file (stdin), which usually corresponds to the terminal keyboard; the standard output file (stdout); and the standard error output file (stderr), both of which correspond to the terminal screen. The process takes input data from standard input files, outputs normal output data to standard output files, and sends error messages to standard error files.
Let's take the cat command as an example. The cat command reads data from a file given on the command line and sends it directly to standard output. If you use the following command:
$ cat config
The contents of the config file will be displayed sequentially on the screen. However, if cat has no arguments on the command line, it reads data from standard input and sends it to standard output. For example:
$ cat Hello world Hello world Bye Bye $
Each line entered by the user is immediately output to the screen by the cat command.
As another example, the command sort reads the file body line by line (from standard input when no file name is given on the command line), sorts it, and sends the result to standard output. The following example reads in a purchase order from standard input and sorts it.
$ sort bananas carrots apples apples bananas carrots $
At this point we get sorted purchase orders on the screen.
Direct use of standard input/output files presents the following problems:
When data is input from the terminal, the user spends half a day to input data that can only be used once. The next time you want to use the data, you have to re-enter it. Moreover, when inputting on the terminal, it is not very convenient to correct if the input is wrong.
The information output to the terminal screen can only be seen but not moved. We cannot do more with this output, such as further processing the output as input to another command.
To solve this problem, Linux introduced two other mechanisms for input and output transfer, namely input/output redirection and pipes.
2. input redirection
Input redirection is the redirection of standard input from a command (or executable) to a specified file. That is, input can come not from the keyboard but from a specified file. So input redirection is primarily used to change the input source of a command, especially if it requires a lot of input.
For example, the wc command counts the number of lines, words, and characters contained in a specified file. If you type only on the command line:
$ wc
Wc waits for the user to tell it what to count, and the shell seems dead. All the text typed from the keyboard appears on the screen, but nothing happens until
If you give a file name as an argument to the wc command, wc returns the number of lines, words, and characters contained in the file, as shown in the following example.
$ wc /etc/passwd 20 23 726 /etc/passwd $
Another way to pass the contents of the/etc/passwd file to the wc command is to redirect wc input. This text forms the content.
>of the here document,which
>continues until the end of
>text delimter
>delim
4 17 98
In the file name. For example:
$ ls > directory.out $ cat directory.out ch2.doc ch3.doc ch4.doc chimp config mail/ test/ $
Save the output of the ls command as a file named directory.out.
Note: If the file following the> symbol already exists, the file will be overwritten.
To avoid specifying files in output redirects that hold only the output redirects of the current command, the shell provides an additional means of output redirection. Output append redirection functions very similar to output redirection, except that output append redirection functions by appending the output result of a command (or executable program) to the *** of a specified file without destroying the original content of the file.
If you want to append the output of a command to a specified file, you can use the append redirect operator>>. The format is: Command>> File Name. For example:
$ ls *.doc>>directory.out $ cat directory.out ch2.doc ch3.doc ch4.doc chimp config mail/ test/ ch2.doc ch3.doc ch4.doc $
As with standard output redirection, error output can be redirected. Use the symbol 2>(or append the symbol 2>>) to indicate redirection to the wrong output device. For example, the following command:
$ ls /usr/tmp 2> err.file
The normal output of the program can be seen on the screen, but any error messages from the program are sent to the file err.file for future inspection.
You can also use another output redirection operator (&>) to send both standard output and error output to the same file. For example:
$ ls /usr/tmp 2> err.file
Redirection groups commands together to achieve new functionality that the system cannot provide individually. For example, use the following command sequence:
$ ls /usr/bin > /tmp/dir $ wc -w At this point, the study of "what are the special characters in the linux shell" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.