Linu study Notes-variables and environment variables

Source: Internet
Author: User
Linu learning Notes & mdash; variables and environment variables abstract: describes the concepts of variables and environment variables. Linu learning notes: variables and environment variables.

Abstract: It mainly records some concepts about variables and environment variables, as well as subprograms and the loading sequence of personal settings files. Note two important commands: export and source.

1. concepts and functions of variables 1.1 and acquisition: 1.1.1 concepts:

A variable is a set of characters or symbols to replace some settings or a string of reserved data.

1.1.2 role:

As described in the concept, we can use a simple item to identify a frequently used and smelly setting or keep data, such as using PATH to indicate the directory where the command is located. you do not need to find a command before executing it, and then use the absolute path to represent the command and execute it.

1.1.3 obtain:

A) you can use the command to display:

Echo $ variableName

Exp:

                        echo $PATH

The content identified by $ PATH is displayed:

                        /root/jdk1.7.0_51/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Use ":" Separate, use in Windows ";" to separate multiple values.

B) you can use the env and set commands to view the environment variables in Linux.

In addition to displaying the default values of the system, you can also display all associated variables! At the same time, it should be noted that if many people are online at the same time, your variables can only be used for yourself (unless you change the system's preset parameter file, such as/etc/profile), without interfering with others!

1.2. variable setting rules

1.2.1 command rules

A) the variable and the variable content are connected with the equal sign "=;

B) the equal signs cannot be directly followed by space characters;

C) the variable name can only contain English letters and numbers, but cannot start with a number;

D) if there is a space character, you can use double quotation marks ("") or single quotation marks (') to combine the variable content. however, pay special attention to the fact that special characters in double quotation marks can retain the variable property, however, special characters in single quotes are only common characters;

E) if necessary, use the escape character "\" to convert special characters (such as Enter, $, \, space character, and ') into general characters;

F) the quote "'command'" can be used for the information provided by other commands in a string of commands '』;

G) if the variable is an expanded variable, double quotation marks ("$ PATH":/home ") and $ variable names must be used to accumulate the variable content;

H) if the variable needs to be executed in other subprograms, you need to use export to make the variable executable, such as "export PATH 』;

(I) generally, uppercase characters are system preset Variables. you can use lowercase characters to set variables by yourself to facilitate judgment (purely based on user interests and hobbies );

J) the method for canceling a variable is "unset variable name 』.

1.2.2 Example

Exp:

Set the name to andyChen

                        name=andyChen

Set the name to andy chen.

                        name=andy\ chen                        name=”andy chen”                        name=’andy chen’

The variable 'whose name is set to the current directory is a key under Esc.

                        name=`pwd`

Note the difference between the following double quotation marks and single quotation marks:

                        name="`pwd`"                        name='`pwd`'

You can use echo $ name to view the comparison results.

                        /root                        `pwd`

Use echo to obtain whether the previous command is correctly executed

                        echo$?

If the output is 0, it is executed correctly; otherwise, an error occurs.

1.3. subroutines and procedures

Run the command bash in the current program to enter the subroutine, and then obtain the variable: name set above. This is the impact of the scope of the variable. The following section describes how to create an ECS instance.

1.4. Scope of variables

After you log on, you get a bash file. all the operations are executed in this bash file. when you execute the bash command again, it enters the subroutine. the variable has no effect here.

1.5 related commands 1.5.1 export command:

Export can change a custom variable to an environment variable! When you use the export command to convert a variable to an environment variable, you can use this variable throughout the environment (valid in this terminal, and invalid when a new terminal is enabled )., a bit like a browser session ).

As mentioned earlier, if export does not add any parameters, it will display all the custom variables and environment variables. here, we will not describe them. you can take a look at them.

1.5.2 source command:

After we append the environment variables to the file, we need to reload the file, and the new changes will take effect, but we can use source + to modify the file or. + to modify the file. The modified file takes effect immediately.

Remember to back up system files before moving them! Otherwise, you can only reinstall the system when the system file is gone or collapsed...

Exp: cp/etc/profile. bak echoname = andyChen>/etc/profile source/etc/profile -- you can also use it. /etc/profile replaces echo $ name
1.5.3 unset command

Variables used to cancel settings:

                        unset name
II. concepts and functions of environment variables 2.1

Compared with variables, an environment variable has two more words: environment, which is very simple. it means that this variable can survive and be used in the entire runtime environment. The entire runtime environment is a terminal interface, including the use of bash. the opened subroutine can also be used, but cannot be used on different terminals. when the terminal is closed, the environment variable disappears, unless it is written in the setup file after logging on to the system, the setting file will be loaded, and the variables in the configuration file will be set to environment variables, so that you can directly use important variables such as PATH without manual setting..

2.2. Scope of environment variables

Files not written: This terminal can be used during the lifecycle. if the terminal is closed or interrupted, the environment variable disappears,

Written to a file: the specified variable is set to an environment variable every time it is started, so it can be used by any terminal. For example, write the NAME under/etc/profile, so that everyone can use this environment variable, of course, it is best to write it into their own settings file, so that only they can use it, but also avoid affecting others.

2.3 convert a variable to an environment variable
                export name 
III. variable-related configuration files and loading sequence 3.1. Introduction to several configuration files 3.1.1 system setting values:

The so-called system setting value, that is, after each user enters the bash shell, the set file will be read first! The preset files include the following:

A)/etc/profile: This file sets several important variables, for example, "PATH, USER, MAIL, HOSTNAME, HISTSIZE, umask", etc, at the same time, the/etc/inputrc file data for keyboard hot construction is also planned. You can set the overall PATH and so on here! At the same time, this file also planned the/etc/profile. d and/etc/inputrc files. among them, inputrc is used for history (historical commands! In short, you can learn about the variable setting method we just learned. you can also set it in this file! However, you must be very careful when setting the file because all users will use the file information.

B)/etc/bashrc: This file is planning the umask function and the prompt character content (PS1 !). Note that this file may be placed in different Linux distribution locations!

C)/etc/man. config: This file may be unrelated to bashshell, but it is also an important file for the system administrator! The content of this archive indicates where to find the man page path when man is used!

This is the file that is often used by the system during configuration! Note that after these files are set up, you must first start logout before starting the configuration!

3.1.2 Personal Settings

Personal Settings

I am using several hidden files in my home directory! These files are used separately! (Note! The files below are hidden files that must be displayed using ls-al 『~ "" Represents the meaning of "home directory:

A )~ /. Bash_profile: defines the file name of the personal PATH and environment variable! You can modify your personal path here! You can also ~ /. Modify it in the personal variable bashrc! Sometimes ~ /. Profile or ~ /. Bash_login and other files to replace this file!

B )~ /. Bashrc: This file is the most important for personal bash settings! Because the file will be loaded at the end.

C )~ /. Bash_history: the purpose of this archive is to record the commands you have used, and when you search for them again or directly using history, you can find the commands you have used! Note that the commands in this execution process will be recorded in this file only after you leave the shell, otherwise, it will only be written to the Cache for temporary storage. you can use the history command to search the records. the number of commands for this file is related to the HISTFILE, you can ~ /. Set in bashrc, or directly set the size in/etc/profile by root!

D )~ /. Bash_logout: This file is "what BASH will do for you when you log out of shell !』 Usually, you only need to clear the screen by default. However, you can also write some backup or other work that you think is important to this file (such as clearing the temporary storage disk ), when you leave Linux, you can solve some things you want to do!

3.2 loading order

A) read/etc/profile first, and then read other additional configuration files, such as/etc/profile. d and/etc/inputrc, based on/etc/profile.

B) read data from different users in the user's home directory ~ /. Bash_profile or ~ /. Bash_login or ~ /. Profile and other setting files

C) read data from different users in the user's home directory ~ /. Bashrc

So after logging on to bash, the setting file will eventually be read ~ /. Bashrc. that is to say, all the variables set in this file will be read.

4. Exercise 4.1: Set an environment variable 4.1.1 that all users can use:
                        echoexport TMPVAR=tem_var >> /etc/profile                        source/etc/profile    
4.1.2 Test Command
                        echo $ TMPVAR                        bash                        echo $ TMPVAR                        su test_user_2                        echo $ TMPVAR

If all outputs are tem_var, the operation is successful.

4.2 set an environment variable 4.2.1 that only one user can use to implement the command:
                        root@ubuntu:~# su test_user_2                        test_user_2@ubuntu:~$ echo exportTUV=test_user_tmp_var >> ~/.bashrc                        test_user_2@ubuntu:~$ source ~/.bashrc
4.2.2 Test Command
                        test_user_2@ubuntu:~$ echo $TUV                        test_user_tmp_var                        test_user_2@ubuntu:~$ exit                        exit                        root@ubuntu:~# echo $TUV
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.