Linux ---- Shell ---- 2 basic syntax

Source: Internet
Author: User

Linux ---- Shell ---- 2 basic syntax 3. Basic Syntax of Shell 1> variables are usually composed of uppercase letters and underscores. Environment variables can be transmitted from the parent process to the child process. Local variables only exist in the current Shell process. You can use the set command to view all the Defined variables (including local variables and environment variables) in the current Shell process) and functions. Environment variables are the concept of all processes, while local variables are unique to Shell. In Shell, the definitions and usage of environment variables and local variables are similar. Define and assign a variable in Shell: -------------------------------- $ VARNAME = value -------------------------------- format Note: No space is allowed on both sides of the equal sign, otherwise, it will be interpreted by Shell as a variable defined by a command or command line parameter. It is a local variable and only belongs to the current Shell. We can use the command export to export the local variable to the environment variable, as follows: export $ export VARNAME = value ---------------------------- you can use the unset command to delete the defined local variables or environment variables, as shown below: ------------------------ $ unset VARNAME refers to the value of the variable: If a variable is named VARNAME, $ {VARNAME} can be used to represent its value. Note: $ is not used when defining a variable. $ is used when getting a variable. Different from the C language, Shell variables do not need to be clearly typed. In fact, the values of Shell variables are strings. For example, we define VAR = 45 and the value of VAR is actually a string of 45, instead of an integer 45. Shell does not need to be defined before use. If a variable value is not defined, its value is an empty string. 2> Replace the file name :? /*/[] Is actually a wildcard. * Match 0 or multiple arbitrary characters? Match a single occurrence of any character [several characters] matching any character in square brackets --------------------------------------------------- $ ls/dev/ttyS * $ ls ch0 ?. Doc $ ls ch0[0-22.16.doc $ ls ch%012%%0-92.16.doc %note: The above matched file names are expanded by Shell. That is to say, Shell first expands all file names following ls that comply with the rules, then all are passed to ls using command line parameters. 3> command substitution: 'or $ () is enclosed by backquotes. Shell runs the command first and then immediately replaces the output result with the current command line. Hour $ DATE = 'date' ====>>>> $ date =$ (DATE) $ echo $ date ---------------------------------------------------- 4> arithmetic substitution: $ (()) for arithmetic calculation, the value of the Shell variable in $ () is converted to an integer, for example: --------------------------------------------- $ VAR = 45 $ echo $ ($ VAR + 3) Attention: $ () can only use the +-*/and () operators, and can only perform integer operations. 5> the conversion string \ is similar to the C language, in order to remove the special meaning of the next single character. 6> the single quotation marks are different from the C language. The single quotation marks and double quotation marks in Shell scripts are the same as the string delimiters (next section of double quotation marks), rather than the character delimiters. Single quotation marks are used to keep the nominal values of all characters in the quotation marks. Even if the \ and carriage return characters in the quotation marks are no exception, the single quotation marks cannot appear in the string. If the quotation marks are not paired, enter the carriage return. Shell will give a prompt to continue the line, asking the user to enclose the quotation marks with a pair. 7> double quotation marks (double quotation marks) are used to keep the literal value of all characters in the quotation marks (carriage return is no exception), except in the following cases: $ the variable name can be used to get the value of the variable. The reverse quotation marks still indicate command replacement. \ $ indicates $ '\' indicates \ in addition to the above situations, the '\' before other characters has no special meaning and only represents the literal value.

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.