The difference and use of Linux environment variables /etc/profile and ~/.bashrc

Use the following command to modify the .bashrc file:

gedit ~/.bashrc

Modify the end of the file:

source /opt/ros/indigo/setup.bash
source ~/catkin_ws/devel/setup.bash

As for what is an environment variable, for a common example, in Windows system, when we install the JDK, if we directly enter java or javac related commands in the command prompt line (cmd),
it will prompt: "java It is not an internal or external command, nor an executable program or batch file." At this time, we need to add the path of the bin directory in the JDK to the PATH variable.

1 Use /etc/profile

 /etc/profile Modifying environment variables is effective for all users, but must source /etc/profile after modification

1) Modify the /etc/profile file

sudo vi /etc/profile

2) Add environment variables at the end of the /etc/profile file

变量名=变量值
...=...
export 变量名 ...
如:JAVA_HOME=/opt/jdk1.8.0_91
  CLASSPATH=.:./bin
  PATH=$JAVA_HOME/bin:$PATH
  export JAVA_HOME CLASSPATH PATH

3) Exit and save

:wq

4) Effective immediately

source /etc/profile

Note: If you do not execute the source command, you need to restart the system to take effect

2 Use .bashrc

 .bashrc Modify this file in a user's home directory, which only takes effect for the current user. It is recommended to use this to have the least impact, and it must be sourced after the modification.

2.1 Add environment variables

 Assume that the environment variable path you want to add is: /opt/my-tools/android-sdk/tools

  • Enter the user's root directory
   cd   $HOME  或 cd ~
  • Then open .bashrc if it does not exist, create a new .bashrc file
vi   .bashrc
  • Add the path you want to add at the end of the .bashrc page
export PATH=$PATH:/opt/my-tools/android-sdk/tools
  • Final execution
source ~/.bashrc

Environment variables are working.

Guess you like

Origin blog.csdn.net/zou_albert/article/details/112368264