Linux variables

 1. Types of variables in Linux

      According to the life cycle of variables, Linux variables can be divided into two categories:

      1. Permanent: The configuration file needs to be modified, and the variable takes effect permanently.

      2. Temporary: use the export command to declare, the variable will be invalid when the shell is closed.

Two, three ways to set variables

      1. Add a variable to the /etc/profile file [effective for all users (permanent)]

      Use VI to add a variable in the file /etc/profile file, the variable will be valid for all users under Linux and is "permanent".

      For example: edit the /etc/profile file and add the CLASSPATH variable

      # vi /etc/profile

      export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

      Note: If you want to modify the file to take effect immediately, you must run # source /etc/profile, otherwise it will only take effect the next time you re-enter the user.

 

But it should be noted that if there are two users, one is swang6 and the other is opscoder, where opscoder is shared by everyone, and swang6 has sudo permissions, then you must use sudo su - opscoder, so that opscoder has /etc/profile The information about the environment variables defined below takes effect.

 

      2. Add variables to the .bash_profile file in the user directory [effective for a single user (permanent)]

      Use VI to add variables to the .bash_profile file in the user directory. The changes are only valid for the current user and are "permanent".

      For example: Edit .bash_profile under the guok user directory (/home/guok)

      $ vi /home/guok/.bash.profile

      Add the following:

      export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

      Note: To take effect immediately after modifying the file, you must run $ source /home/guok/.bash_profile, otherwise it will only take effect when the user is re-entered next time.

      3. Directly run the export command to define variables [only valid for the current shell (BASH) (temporary)]

      Use [export variable name=variable value] directly on the command line of the shell

      Define a variable. The variable is only valid under the current shell (BASH) or its subshell (BASH). If the shell is closed, the variable will be invalid. When a new shell is opened, the variable will not exist. If you need to use it, you need to use it. redefine.

3. PATH declaration, its format is:

      PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N>

      You can add the specified path yourself, separated by a colon. After the environment variable is changed, it will take effect the next time the user logs in.

      If you want to take effect immediately, you can execute the following statement: $source .bash_profile

      It should be noted that it is best not to put the current path "./" in the PATH, which may be subject to unexpected attacks.

      Once done, the current search path can be viewed via $ echo $PATH. After this customization, you can avoid frequently starting programs located outside the path searched by the shell.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327070385&siteId=291194637