Linux shell command line parameter usage _linux shell

Source: Internet
Author: User
Tags echo command

It is customary to use the Linux command line to manage Linux systems, for example:

Copy Code code as follows:

$ date
211 01:34:58 CST 1999
$

When the user logs in, it actually goes into the shell, and it follows a certain syntax to interpret the commands that are entered and pass them to the system.
The first word entered in the command line must be the name of a command, the second word is the command's option or parameter, and each word in the command line must be separated by spaces or tabs, in the following format:
Copy Code code as follows:
$ Command Option Arguments

One, options and parameters

Options are code that includes one or more letters, preceded by a minus sign (minus is necessary, Linux uses it to distinguish between options and parameters), and options can be used to change the type of action performed by the command. For example:

Copy Code code as follows:
$ ls
MOTD passwd

This is the LS command without options, which lists all the files in the current directory, lists only the names of individual files, and does not display additional information.

Copy Code code as follows:
$ ls-l
Total 2
-rw-r--r--2 Wzh book APR 20:37 MOTD
-rw-r--r--2 Wzh book 796 Apr 20:37 passwd

By adding the-l option, a line of information is listed for each file, such as the size of the data and when the data was last modified.

Most commands are designed to accept parameters. A parameter is one or more words that you type after an option in the command line, such as:

Copy Code code as follows:
$ ls-l Text
-rw-r--r--2 Wzh book APR 20:37 MOTD
-rw-r--r--2 Wzh book 796 Apr 20:37 passwd
$

All files under the text directory and their information are displayed.

Some commands, such as LS, can take parameters, while some commands may require a minimum number of parameters. For example, the CP command requires at least two parameters, and if the number of parameters does not match the command request, the shell will give an error message. For example:

Copy Code code as follows:
$ cp-i MyData NewData

Note: The options on the command line precede the parameter input.

Second, command line features
The command line is actually a text buffer that you can edit, and you can edit the text you entered before you press ENTER. For example, the use of the backspace key can delete just typed characters, you can delete the entire line, you can also insert characters, so that users in the input commands, especially complex commands, if there is a typing error, no need to re-enter the entire command, as long as the use of editing, you can correct the error.

The up arrow allows you to display the command you just executed, which allows you to repeat the previously executed command without having to retype the command.

Bash holds a list of previously typed commands, called the Command history table. By pressing the UP arrow, you can display each command on the command line at a successive order. Also, press the DOWN arrow to move down in the list of commands so that previous commands can be displayed on the command line, and the user can modify and execute the commands. This feature will be discussed in detail in section 10.4.

You can also place multiple commands on one command line, separating each command with a semicolon. For example:

Copy Code code as follows:
$ ls-f;cp-i MyData NewData


You can also enter a command on several command lines and use a backslash to persist a command line to the next line.

Copy Code code as follows:
$ cp-i/
MyData/
NewData


The CP command above is entered in three lines, the first two lines end with a backslash, and three lines as a command line.

Special characters in the shell

In addition to using ordinary characters in the shell, you can also use special characters that have special meanings and features. When using them, you should pay attention to their special meaning and scope of action. These special characters are described separately below.
Three, wildcard characters

Wildcard characters are used for pattern matching, such as filename matching, path name search, string lookup, and so on. Commonly used wildcard characters are *,?, and the character sequence enclosed in brackets []. The user can include these wildcard characters in the filename as a command parameter, forming a so-called "pattern string" and performing pattern matching during execution. The slash (/) must be explicitly matched. For example, "*" cannot match. File, and ". *" can match. File.
? Represents any single character.
[] represents the specified range of characters, so long as the characters in the [] position in the file name are within the range specified in [], then the file name matches the pattern string. The range of characters in square brackets can consist of characters that are given directly, or can consist of a starting character, a terminating character, and an intermediate hyphen (-) that represents a qualified range. For example, F [a-d] has the same effect as f [ABCD]. The shell will take all the file names that match the pattern string specified in the command line as the parameters of the command, form the final command, and then execute the command.

The specific meaning of these wildcard characters.
Iv. the meaning of pattern string
Copy Code code as follows:

* The name of all files in the current directory.
*text* the name of a file containing Text in all file names under the current directory.
[ab-dm]* the name of all files that begin with a, B, C, D, and M under the current directory.
[AB-DM]? The name of all files in the current directory that begin with a, B, C, D, and M followed by only one character.
/usr/bin/?? The name of all two-character files under the directory/usr/bin.


Note that the hyphen "-" is only valid in square brackets, representing a range of characters, such as outside square brackets, that becomes the normal character. and * and? Just outside the square brackets are wildcard characters, and if they appear within square brackets, they also lose the ability of the wildcard character to become the ordinary. For example, the pattern "-a[*?] Only one pair of brackets in ABC is a wildcard, * and? are common characters, so it matches only the string-a*abc and-a?abc.

Finally, some problems needing attention when using wildcard characters are explained. Because *,?, and [] have a special meaning for the shell, these characters should not appear in normal file names. In particular, do not appear in the directory name, otherwise the shell match may be infinite recursion down. Also note that if the directory does not have a filename that matches the specified pattern string, the shell will use the pattern string itself as an argument to the command. This may be the reason why special characters appear in the command.

Five, quotes
Quotes in the shell are divided into three types: single quotes, double quotes, and inverted quotes.

Six, single quotes '
A character characters, enclosed in single quotes, appears as a normal character. Special characters, which are enclosed in single quotes, also lose their original meaning and are interpreted only as ordinary characters. For example:
Copy Code code as follows:
$ String= ' $PATH '
$ echo $string
$PATH

Visible $ maintains its own meaning, appearing as ordinary characters.

Seven, double quotes "
Characters enclosed in double quotes, except that the $,/, ', and ' characters are still special and retain their special features, the remaining characters are treated as normal characters. In the case of $, the variable and $ are replaced with the value of the variable that is subsequently specified, and for/, the escape character, which tells the shell not to treat the character after it specifically, as a normal character. As you can imagine, in double quotes you need to precede with/only four characters $,/, ' and ' itself. And for the "number, if it is not preceded by/, the shell will match it with the previous" number.

For example, suppose the value of path is.:/ Usr/bin:/bin, enter the command:

Copy Code code as follows:

$ teststring= "$PATH///"/$PATH "
$ echo $TestString
.:/ usr/bin:/bin/"$PATH

You can try a little bit before the second double quote without/what will happen.

Eight, inverted quotes '
Reverse quotation marks (') the key for this character is generally located in the upper-left corner of the keyboard, and do not confuse it with single quotes ('). A string enclosed in quotes is interpreted by the shell as the command line, and at execution time the shell executes the command line first and replaces the entire inverted quotation mark (including two inverted quotes) with its standard output. For example:
Copy Code code as follows:

$ pwd
/home/xyz
$ string= "current directory is ' pwd '"
$ echo $string
Current Directour is/home/xyz


When the shell executes the echo command, it first executes the command pwd in ' pwd ' and replaces the output/home/xyz the ' pwd ' part, and finally outputs the entire result of the replacement.
This feature of the inverted quotation mark allows for command substitution, which is to assign values to the specified variable in the enclosed quotation marks. For example:

Copy Code code as follows:

$ Today= ' Date '
$ echo Today is $today
This is Mon Apr 16:20:13 CST 1999

The inverted quotation marks can also be nested. However, it is important to note that the inverted quotation marks inside the nested use must be escaped with a backslash (/).
For example:
Copy Code code as follows:

$ Abc= ' echo the number of users is/' who| wc-l/'
$ echo $ABC
The number of users is 5


Special characters of the shell can also be used in the command line between the inverted quotes. Shell to get the result of the command in ', it actually goes to execute the command specified in '. When executed, the special characters in the command, such as $, "," and so on will have special meaning, and "' may contain any legitimate shell commands, such as:

Copy Code code as follows:

$ ls
Note Readme.txt Notice Unix.dir
$ teststring= "' echo $HOME ' ls [nn]* '"
$ echo $TestString
/HOME/YXZ Note Notice

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.