Basic Shell programming syntax in Linux

Source: Internet
Author: User
The basic syntax of Shell is similar to the advanced programming language. Shell also provides instructions and functions for Using Variables. for Shell, the value of all variables is a string, and the Shell program uses the form of $ var.

Shell Basic syntax

Like the advanced programming language, Shell also provides instructions and the function of using variables. for Shell, the value of all variables is a string, the Shell program uses $ var to reference the value of the variable named var.

Shell has the following basic types of variables:

(1) environment variables defined by Shell:

At the beginning of execution, Shell has defined some variables related to the system's working environment. you can also redefine these variables. common Shell environment variables include:

HOME is used to save the full path name of the registration directory.

PATH is used to save the directory PATH names separated by colons. Shell searches these directories in the order given in the PATH variable, and the first executable file with the same name as the command will be executed.

The type of the TERM terminal.

The identifier of the current user. The value is a string consisting of digits.

The absolute path name of the current working directory of PWD. The value of this variable varies with the use of the cd command.

PS1 primary prompt. for privileged users, the default primary prompt is #. for common users, the default primary prompt is $.

When the PS2 Shell receives the user's input command, if the user enters "" at the end of the input line, then press Enter, or when the Shell determines that the command entered by the user is not over when the user presses the enter key, the auxiliary prompt is displayed, prompting the user to continue entering the rest of the command. the default auxiliary prompt is>.

(2) user-defined variables:

You can define your own variables according to the following syntax rules:

Variable name = variable value

Note that $ should not be added before the variable name when defining the variable, and $ should be added before the variable name when referencing the variable content; when assigning values to the variable, no space must be left on both sides of the equal sign. if the variable itself contains spaces, the entire string should be enclosed in double quotation marks.

When writing a Shell program, we recommend that you use uppercase letters to indicate all variable names to distinguish them from command names.

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 ensure the read-only of a variable:

Readonly variable name

At any time, the created variables are only local variables of the current Shell, so they cannot be used by other commands or Shell programs run by the Shell, the export command can provide a local variable to other commands executed by Shell in the format:

Export variable name

You can also use the export command when assigning values to variables:

Export variable name = variable value

Variables described in export can be accessed in all commands or programs run after Shell.

(3) location parameters:

A location parameter is a variable that is determined by its location in the command line that calls the Shell program. it is a parameter entered after the program name. Location parameters are separated by spaces. Shell replaces $1 with the first location parameter in the program file, the second with $2, and so on. $0 is a special variable whose content is the file name of the current Shell program. Therefore, $0 is not a bitwise parameter, $0 is not included when all the current location parameters are displayed.

(4) predefined variables:

Predefine variables are similar to environment variables and are also defined at the beginning of Shell. The difference is that you can only use these variables according to Shell definitions, rather than redefinition. All predefined variables are composed of the $ operator and another symbol. common Shell predefined variables include:

$ # Number of location parameters.

$ * Content of all location parameters.

$? Status returned after the command is executed.

$ Process ID of the current process.

$! The last process number that runs in the background.

$0 indicates the name of the currently executed process.

Where, $? Used to check whether the previous command is correctly executed. (In Linux, if the exit status of a command is 0, the command is correctly executed. if the value is not 0, an error occurs .)

$ The most common purpose of a variable is to use the name of the temporary file to ensure that the temporary file will not be repeated.

(5) variable for parameter replacement:

Shell provides the parameter replacement function so that you can assign different values to variables based on different conditions. There are four types of parameters for parameter substitution. these variables are usually associated with a location parameter and determine the value of the variable based on whether the specified location parameter has been set to a class, their syntax and functions are as follows.

A. variable =$ {parameter-word}: If a parameter is set, replace the value of the parameter with the value of the parameter. otherwise, replace the value with word. That is, the value of this variable is equal to the value of a parameter. If this parameter is not set, the variable is equal to the value of word.

B. variable =$ {Parameter = word}: If a parameter is set, replace the value with the value of the parameter. otherwise, set the variable to word and replace the value with word. Note that location parameters cannot be used in this way, because location parameters cannot be assigned values in Shell programs.

C. variable =$ {parameter? Word}: If a parameter is set, replace the value of the variable with the value of the parameter. Otherwise, the word is displayed and exited from the Shell. if word is omitted, the standard message is displayed. This variable must be equal to the value of a parameter. If this parameter is not set, a message is displayed and then exited. Therefore, this method is often used to indicate errors.

D. variable =$ {parameter + word}: If the parameter is set, replace the variable with word. otherwise, do not replace the variable.

The "parameter" in all four forms can be either a location parameter or another variable, but there are many situations where the location parameter is used.

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.