Shell variable function (1)

Source: Internet
Author: User
Shell variable function (1) Shell variable function: the so-called variable is to replace a complicated and easy-to-change data with a simple thing. The so-called environment variable is that we log on to a user name correctly, and the system gives us a bash execution program so that we can actually access B... shell variable function (1) Shell variable function: the so-called variable is to replace a complicated and easy-to-change data with a simple thing. The so-called environment variable is that when we log on to a user name correctly, the system gives us a bash execution program so that we can truly communicate with the system through bash, the system needs some variables to provide access to its data (or set the parameter values of some environments, such as whether to display the color or not .) These environment variables include: PATH/HOME/MAIL/SHELL. here, the environment variables are capitalized only to distinguish them from custom variables. Summary: What is a variable? A variable replaces some settings or a string of reserved data with a set of characters or symbols. Use echo to view the variable: echo $ variable to view the variable echo $ PATH. the following two ways are to view the PATH echo $ {PATH} to cultivate your own abilities: think of echo $ HOME // The directory where you can view the environment. for example:/home/xwg echo $ MAIL // The MAIL path is eg: /var/mail/xwg or/var/spool/mail/xwg modify and set the variables: only the trilogy is required. assume that myname is not set to echo $ myname = xwg echo $ myname xwg in bash. // Display information xwg. at this time, the variable myname contains the xwg data.. As you can see above, in bash, when a variable name has not been set, the default content is blank. The following are the rules set for variables: 1. variables are connected with the variable content with an equal sign "=". 2. there cannot be spaces on both sides of the equal sign. 3. variable names can only be English letters or numbers, but it cannot start with a number. 4. if there is a space character in the variable content, double quotation marks or single quotation marks can be used to combine the content of the variable. However, special characters in double quotation marks, such as $, can keep the original feature eg: directly type var = "lang is $ LANG" echo $ var in the terminal to get the same lang is zh_CN.UTF-8 if lang is $ LANG is in single quotes, you can only get lang is zh_CN.UTF-8 5, you can use the escape character "\" will be special symbols such as [ENTER], $, \, space character ,! Etc.] 6 ,??? Run "version = $ (uname-r)" to check the kernel parameter echo $ version. there is no result at all ??? 7. if the variable is added to the variable content, you can use "$ variable name" or "$ {variable} to accumulate the content eg:" PATH = "$ PATH ": /home/bin "// tested, but the result shows that Directory 8 is not found. if the variable needs to be executed in other sub-processes, use export to change the variable to the environment variable eg: "export PATH" // do not understand? 9. generally, uppercase characters are the default system variables. you can use lowercase characters to set variables for easy judgment. 10. use unset + variable name eg to cancel the setting of myname: unset myname to set the variable case: 1. add the/home/dmtsai/bin directory in the PATH variable. three methods are described: PATH = $ PATH:/home/dmtsai/bin PATH = "$ PATH ": /home/dmtsai/bin PATH =$ {PATH}:/home/dmtsai/bin: echo $ PATH 2. how do I use the variable name = xwg in the next shell? // Set the variable echo $ name // Display xwg bash // enter the so-called subprocess echo $ name // sub-process: echo again, there is no content just set to exit // exit the sub-process export name // bash // enter the sub-process echo $ name // sub-process: execute here, with the exit sub-process Is to open another new shell in the current case, the new shell is a sub-process. Generally, the custom variables of the parent process cannot be used within the child process. However, after the variables are changed to environment variables through export, they can be used in sub-processes. 3. how to enter the module directory of your current kernel: cd/lib/modules/'uname-r'/kernel cd/lib/modules/$ (uname-r) /kernel uname-r can obtain the basic information of the version, and use the cd command to smoothly enter the location of the driver of the current kernel. The above two operations are performed: the first step is to first perform the uname-r operation in the single quotes, and get the kernel version information. The second step is to bring the above results into the original command. Here we can see the power of the anti-single quotes. the content in ''will be executed first, as described in the above example and below: ls-l 'locate crontab' // You can view the permissions of each crontab-related file name. if a directory is frequently used, for example, cd/home/xwg/accept/unix, there are two ways to simplify your practice: method 1: use the alias cdd = "cd/home/xwg/accept/unix/linux/learn". However, this method has the disadvantage that it can be used by other users, you cannot change the terminal. Method 2: set variables, work = "cd/home/xwg/accept/unix/linux/learn" cd $ work // The use of export can be used to implement sub-processes, as described above (specific functions need to be discussed) "version = $ (uname-r)" replace "version = 'uname-R'" is better, and it is not easy to see errors or make mistakes.
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.