Tips for using variables in Unix systems (1)

Source: Internet
Author: User

Variables are indispensable in any development language. In Unix operating systems, there are also variables. System Engineers can use variables when writing scripts or commands. Now I will talk to you about how to use variables in Unix operating systems, and under what circumstances to use variables.

I. Considerations for variable definition.

Defining variables in Unix is much easier than defining variables in other languages. Because variables used in other languages must be declared in advance, but not in Unix operating systems. In addition, when defining a variable in a Unix operating system, you do not need to specify the data type it uses. The system automatically determines based on the input content. Therefore, it is very convenient to use. For example, to define an integer variable, you only need to use the following format: int1 = 1981. Note: although the variable is assigned an integer, it is stored as a string. That is to say, so far, all Unix system variables are variable type variables. This may change in later versions of the operating system. But so far, all shell variables are variable types. This is a principle that System Engineers must keep in mind when using variables.

However, when defining variables, I suggest that you follow the following specifications.

First, pay attention to the Case sensitivity of variables. Although there are no special requirements on the Case sensitivity of variables in Unix systems, both upper and lower case are feasible. However, there is indeed an unwritten rule in the industry. That is, the variable names used by the operating system and the software development kit are expressed in uppercase letters. The variables used in your script programs should be expressed in lower case. This is mainly based on the readability of variables. Distinguish User-Defined variables from system variables. For example, the PATH and other stage variables in the operating system are all in lowercase. Although this is not a mandatory specification, I hope that readers can still abide by this specification.

Second, there is a format requirement during variable initialization. For example, in C shell, there is a strict requirement. As in the above definition, no space is used for both sides of =. If you want to use a space on the left side, you must also use a space on the right side of the equal sign. That is, the left and right sides must be symmetric. Otherwise, the system will not be able to recognize this variable. Some engineers may have experience programming in other languages and they do not have such demanding requirements in this regard. However, if a variable is used in a Unix operating system, this rule must be followed, that is, when a value is assigned to a variable, either there is no space on both sides of the equal sign or a space must be added.

Third, note that there is no constant concept in Unix operating systems, but only the read-only variable concept. In fact, the two principles are the same, but the names are different. If the system engineer needs to define a read-only variable, add a keyword readonly before the variable. After this keyword is added, you can prevent this variable from being modified. Note that even the user who created this variable cannot modify the variable. In fact, this variable has changed to a constant. Unless you use the unset keyword to explicitly Delete this read-only variable, and then recreate it. Otherwise, the read-only variables cannot be modified after they are created, including the owner of the variables.

Fourth, in Unix operating systems, variables are referenced in different languages. Whether it is a system variable or a user variable, you must add a special metacharacter ($) before the variable name When referencing the variable. The system considers this variable only after the metacharacter is added. To assign the value of the preceding variable to another variable, the following format must be used: int2 = $ int1. This format must also be used when the echo command is used to display User variables and environment variables, such as echo $ PATH.


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.