Three methods set variable linux

1 variables in the / etc / profile file to take effect for all users (permanent)

VI with an increase in variable file / etc / profile files, this variable will be effective for all users of Linux, and is "permanent."

For example: editing / etc / profile file, add the CLASSPATH variable

# Vi / etc / profile

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

Note 1: The profile file will be run at system startup. You can add other commands on the inside, but we must add correct, otherwise the system will not start up.

2 increase in the .bash_profile file in the user directory variables for a single user to take effect (permanent)

VI with an increase in the .bash_profile file in the user directory variable, the amount of change will only current user, and is "permanent."

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

# We /home/guok/.bash.profile

Add the following:

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

Note 2: If you modify / etc / profile, then perform value source profile or execute command ./profile,PATH point after the end of the editing will take effect immediately. The principle of this method is to perform another / etc / profile shell script, note that if sh / etc / profile is not enough, because sh is executed in the sub-shell process, even if the PATH does not change the response to the current environment , but the source is executed in the current shell process, so we can see the change in the PATH.

Note 3: When a variable defined repeatedly, to the back of the first set.

  For example: In the default file peofile the PATH variable has set PATH = ¥¥¥¥¥¥¥, in the future may be in the PATH settings, are generally added at the end PATH profile file = ······ (Analogy ). And into the system finds PATH = ······· ¥¥¥¥¥¥¥¥¥, that is the same name of the environment variable, after writing the first act (generally speaking).

Note 4, special characters introduced.

  In the following example, the profile described by the use of special symbols below.

  export A=/q/jing:aaa/cc/ld

  export B=.:/liheng/wang export A=/cd/cdr:$A

  : Meaning that parallel, a plurality of variable values ​​such as A, with: separating symbols.

  . Indicates that the current directory of your operation. For example pap command looks for the B environment variable.

In / home type B command, the system first (ie, the current path) to find content on B in the / home directory, if not to find content in the B / liheng / wang directory. $ Indicate values ​​before the current definition of the variable, e.g. $ A Representative / q / jing: aaa / cc / ld. That is to say A = / cd / cdr: / q / jing: aaa / cc / ld

Note 5, the common environment variables

PATH: the shell to determine which directories to find commands or programs

  HOME: The current user's home directory

    MAIL: refers to the current user's mail store directory.

    SHELL: refers to the current user of what type of Shell.

  HISTSIZE: it refers to the number of saved history command records.

  LOGNAME: it refers to the current user's login name.

HOSTNAME: refers to the name of the host, if many applications to use a host name, it is usually from this environment variable to get in.

  LANG / LANGUGE: language and is related environmental variables, multilingual user can modify this environment variable.

  PS1: basic prompt, be # for the root user, for the average user is $.

  PS2: is a subsidiary of the prompt, the default is ">." Can be modified by modifying the current command operator environment variable, for example the following command will be modified to prompt string "Hello, My NewPrompt :)".

  # PS1=" Hello,My NewPrompt :) "

3 runs directly export command to define variables [only valid (provisional) for the current shell (BASH)]

In the command shell directly [export variable name = variable value] defined variable that only the current shell (BASH) or a sub-shell (BASH) is valid, a closed shell, also fails variable do not have this variable, and then open a new shell, also you need to use the words need to be redefined.

 

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11109258.html