Linux system shell command common features use detailed

Source: Internet
Author: User

The shell of a Linux system is not just a command, but an interpreter of other commands. You can debug other commands to complete the compilation. So how should shell commands be used in Linux systems?

In fact, as a command language, the interactive interpretation and execution of user input commands is only one aspect of shell functionality, and the shell can also be used for programming, which provides a means of defining variables and parameters and a rich program control structure. Using shell programming is similar to a batch file in DOS, called shell script, or shell program or shell command file.

Shell Basic Syntax

Like advanced programming languages, the shell provides the ability to illustrate and use variables. For a shell, the value of all variables is a string, and the shell program uses $var to refer to the value of the variable named var.

The shell has several basic types of variables.

(1) Environment variables defined by the shell:

The shell has defined some variables related to the working environment of the system at the beginning of execution, and the user can redefine these variables, and the common shell environment variables are:

Home is used to save the full path name of the registered directory.

Path is used to hold the directory pathname separated by colons, and the shell will search these directories in the order given in the path variable, and the first executable file found to be consistent with the command name will be executed.

Type of TERM terminal.

UID The current user's identifier, which is a string consisting of digits.

PWD the absolute pathname of the current working directory, and the value of the variable varies with the use of the CD command.

PS1 the primary prompt, under privileged users, the default primary prompt is #, and under normal users, the default primary prompt is $.

PS2 when the shell receives user input commands, if the user enters at the end of the input line and then returns, or the shell determines that the command entered by the user is not finished when the user presses ENTER, prompts the user to continue to enter the remainder of the command, and the default secondary prompt is ".

(2) User-defined variables:

Users can define their own variables according to the following syntax rules:

Variable name = variable Value

The point to note is that when you define a variable, variable name should not be signed $, when referencing the contents of a variable should be in front of the variable name plus $; When assigning a value to a variable, you must not be left blank on both sides of the equal sign, if the variable itself contains a space, the whole string should be enclosed in double quotes.

When writing a shell program, in order to differentiate between variable and command names, it is recommended that all variable names be represented in uppercase letters.

Sometimes when we want to describe a variable and set it to a specific value without changing its value, we can use the following command to guarantee the read-only of a variable:

ReadOnly variable Name

At any time, the variables created are only local variables of the current shell, so they cannot be exploited by other commands or shell programs that the shell runs, and the Export command can provide a local variable to other commands executed by the shell, in the form of:

Export variable Name

You can also use the Export command while assigning a value to a variable:

Export variable name = variable Value

Variables that are described with export can be accessed in all commands or programs that are run later in the shell.

(3) Position parameter:

A positional parameter is a variable that is determined at its own location in the command line that calls the shell program, and is the argument entered after the program name. The position parameters are separated by a space, the shell takes the first position parameter replaces the $ in the program file, the second replaces $, and so on. The $ $ is a special variable whose contents are the filename of the current shell program, so the $ $ is not a positional parameter and does not include the $ $ when displaying all current positional parameters.

(4) Predefined variables:

A predefined variable is similar to an environment variable and is a variable that is defined at the beginning of the shell. The difference is that users can only use these variables based on the definition of the shell, not redefine it. All predefined variables are made up of $ and another symbol, and commonly used shell predefined variables are:

Number of $# position parameters.

$* the contents of all positional parameters.

$? The state returned after the command was executed.

$$ the process number of the current process.

$! The last process number to run in the background.

The name of the currently executing process for $.

Where, $? is used to check that the previous command is performing correctly. (In Linux, a command exit status of 0 indicates that the command executes correctly, and any non-0 value indicates a command error.) )

The most common use of $$ variables is using the name of the staging file to ensure that the staging file does not repeat.

(5) variable for parameter substitution:

The shell provides a parameter substitution feature so that users can assign different values to variables based on different conditions. There are 4 variables for parameter substitution, which are usually associated with a positional parameter, and their syntax and functionality are as follows, depending on whether the specified positional parameter has been set to determine the value of the variable.

A. Variable =${parameter-word}: If a parameter is set, the value of the variable is replaced with the values of the parameter, otherwise the word is replaced. That is, the value of this variable equals the value of one parameter, and if the argument is not set, the variable equals the value of Word.

B. Variable =${parameter =word}: If you set a parameter, replace the value of the variable with the value of the parameter, otherwise the variable is set to Word, and then the value of the parameter is replaced with Word. Note that positional parameters cannot be used in this way, because positional parameters cannot be assigned to a shell program.

C. variable =${parameter? Word}: If a parameter is set, the value of the variable is replaced with the value of the parameter, otherwise the word is displayed and exited from the shell, and if Word is omitted, the standard information is displayed. This variable requirement must be equal to the value of a parameter. If the parameter is not set, a message is displayed, and then exits, so this is often used for error indications.

d. Variable =${parameter +word}: If the parameter is set, replace the variable with Word, otherwise no substitution occurs.

All of the 4 forms of "parameters" can be either positional or another variable, but more with positional arguments.

The above is the use of the shell command tutorial, you can say that the shell command is a Linux system essential programming tools.

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.