简体   繁体   中英

can not set linux environment variables as I expect

I open two terminals. In first terminal:

export CLASSPATH="abc"
printenv CLASSPATH   ---> output is abc

then in second terminal:

printenv CLASSPATH   ---> no output 

why in second terminal I dont have the variable?

It's not going to work because each program inherits environment, that is a list of environment variables and their values from their parent process. Environment is not automatically propagated to all other programs on the system but is only inherited by children of the given program. To set a global environment that would work in all newly opened terminals you need set it in the file that is sourced each time you open the terminal. What file would that be depends on what shell you use and your system local setup. For example, if you use bash you should put export CLASSPATH="abc" in ~/.bashrc.

For accessing global variable you need to put $ before it. Are you doing that?

try echo $CLASSPATH

I think you will find this helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM