Linux tutorial -- shell command

Source: Internet
Author: User
Article title: Linux tutorial-shell command. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Command line
  
When you log on to Linux, you can see a shell prompt that marks the start of the command line. You can enter any commands and parameters at the end of the prompt. For example:
  
$ Date
  
November 23 01:34:58 CST 1999
  
$
  
When a user logs on, the user actually enters the shell, which follows certain syntax to explain the input command and pass it to the system. The first word entered in the command line must be the name of a command, and the second word is the option or parameter of the command. each word in the command line must be separated by spaces or tabs. the format is as follows:
  
$ Command Option Arguments
  
  
■ Options and parameters
  
The option is a code that contains one or more letters. it has a minus sign (minus sign is necessary, and Linux uses it to differentiate options and parameters ), option can be used to change the type of the command execution action. For example:
  
$ Ls
  
Motd passwd
  
$
  
This is an ls command without any options. it can list all files in the current directory, only the names of each file are listed, and no more information is displayed.
  
$ Ls-l
  
Total 2
  
-Rw-r -- 2 wzh book 22 Apr 20 20:37 motd
  
-Rw-r -- 2 wzh book 796 Apr 20 20:37 passwd
  
$
  
The-l option is added to list a row of information for each file, such as the data size and the last modification time.
  
Most commands are designed to accept parameters. A parameter is one or more words entered after the option in the command line, for example:
  
$ Ls-l text
  
-Rw-r -- 2 wzh book 22 Apr 20 20:37 motd
  
-Rw-r -- 2 wzh book 796 Apr 20 20:37 passwd
  
$
  
All files in the text directory and their information are displayed.
  
Some commands, such as ls, can contain parameters, while some commands may require some minimum parameters. For example, the cp command requires at least two parameters. if the number of parameters does not meet the Command requirements, shell will provide error information. For example:
  
$ Cp-I mydata newdata
  
Note: Options in the command line are input prior to parameters.
  
  
■ Command line features
  
The command line is actually a text buffer that can be edited. you can edit the input text before pressing enter. For example, you can use the BACKSPACE key to delete the just-typed characters, delete the entire line, and insert characters so that when you enter commands, especially complex commands, an error occurs, you do not need to re-enter the entire command. you can use the edit operation to correct the error.
  
The up arrow can be used to re-display the just-executed command. This function can be used to re-execute the previously executed command without re-typing the command.
  
Bash stores the list of commands you have previously typed. This list is called the Command History table. Click the arrow to display each command on the command line. Similarly, you can move the down arrow in the command list to display previous commands on the command line. you can modify and execute these commands. This feature is described in detail in Section 10.4.
  
Multiple commands can also be placed in a command line and separated by semicolons. For example:
  
$ Ls-F; cp-I mydata newdata
  
You can also enter a command in several command lines and use a backslash to continue a command line to the next line.
  
$ Cp-I \
  
Mydata \
  
Newdata
  
   
  
The preceding cp command is input in three lines. The first two lines end with a backslash and use the three lines as one command line.
  
Special characters in shell
  
In shell, in addition to common characters, you can also use special characters with special meanings and functions. When using them, pay attention to their special meanings and scopes. The following describes these special characters.
  
  
■ Wildcard
  
Wildcard characters are used for pattern matching, such as file name matching, path name search, and string search. Common wildcard characters include *,? And the character sequence enclosed in square brackets. You can include these wildcards in the file name of the command parameter to form a so-called "pattern string" and perform pattern matching during execution.
  
* Represents any string (length can be different). For example, "f *" matches any string that starts with "f. However, it should be noted 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.
  
The following table describes the meanings of these wildcards.
  
  
  
■ Mode string
  
  
Yi
  
  
  
*
  
  
The names of all files in the current directory.
  
  
  
* Text *
  
  
The 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, and m in the current directory, followed by only one character.
  
  
  
/Usr/bin /??
  
  
The names of all files with two characters under the/usr/bin directory.
  
  
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 * 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, the mode "-a [*?] Only one pair of square brackets in abc is a wildcard, * 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, the 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.
  
  
■ Single quote'
  
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.
  
  
■ Double quotation marks
  
Characters enclosed by double quotation marks, except the characters $, \, ', and "are still special characters and keep their special functions. Other characters are still treated as common characters. For $, it is to replace this variable and $ with the variable value specified later. 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. If no \ is added before the pair, Shell will match it with the previous one.
  
For example, assume that the PATH value is.:/usr/bin:/bin and enter the following command:
  
$ TestString = "$ PATH \" \ $ PATH"
  
$ Echo $ TestString
  
.:/Usr/bin:/bin \ "$ PATH
  
$
  
You can try the result by yourself without adding \ before the second double quotation mark.
  
   
  
  
■ 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 single quotation marks. The string enclosed by backquotes is interpreted by shell 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. 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 in the command line between backquotes. Shell is used to get the result of the command in ''. it actually needs to execute the command specified in. Special characters in the command, such as $ ,",? And can contain any legal Shell command, such:
  
$ Ls
  
Note readme.txt Notice Unix. dir
  
$ TestString = "'echo $ home' 'ls [nN] * '"
  
$ Echo $ TestString
  
/Home/yxz note Notice
  
$
  
  

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.