Knowledge about Ubuntu environment variables

When using source code to install software under Linux, you can usually only use the software command in the software installation directory (except for installation using the yum command). If you want to use it globally, you can add the software installation path to the system environment variable.


Assume it is aa.sh in the /home/myfile/run/script directory. If you want to run a certain command in all directories, you can use the following methods:

  1. ln -s aa.sh (the location where aa.sh is stored) /bin/aa.sh (aa.sh after bin indicates which command you enter to run this script)
  2. Copy aa.sh directly to /bin
  3. Add the directory where the command is located to the environment variable

The first two methods are mainly because the /bin directory is already in the system's environment variables. The following mainly talks about how to add environment variables.


1. Several methods of adding environment variables

  1. Use the export command to define variables [valid only for the current shell (BASH) (temporary)]
export PATH=/usr/local/webserver/php/bin:$PATH
export PATH=${
    
    PATH}:/home/myfile/run/script

When you execute a command like this, the system will search in this directory.

The usage method of export is:

export PATH=$PATH:路径1:路径2:路径n;

$PATH is a system variable, which represents all previously set paths. If not added, all previous paths will be invalid. So it must be added.

  1. Modify the configuration file /etc/profile [effective for all users (permanent)]
sudo vim /etc/profile
#要让修改马上生效,需要在修改后source一下
source /etc/profile
  1. Add variables in the .bash_profile file in the user directory [effective for the current user (permanent)]
#也可以修改/etc/rc.local文件和/root/.bashrc文件
source .bash_profile
  1. Add the command to ~/.bashrc, and the current user can use it directly as soon as he logs in.
$ vi /home/li/.bashrc
#添加如下内容:
#export 代表声明路径变量,其中每个路径以冒号:分割
export PATH=PATH:新软件路径
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar;$JAVA_HOME/lib/dt.jar

Note: If you want the modified file to take effect immediately, you must run source /home/li/.bashrc. Otherwise, it will only take effect the next time you log in as this user.

  1. Modify environment variables directly
sudo vim /etc/environment
#注销或者重启可以使修改生效,如果要使添加的环境变量马上生效可以采取以下方式:
source /etc/environment

The difference that takes effect immediately

source profile
#命令使得当前脚本文件命令生效,即执行脚本的内容,相当于 
. profile

The environment variable value run through the above two methods will be passed to the parent process.
The source command is also called the "dot command", which is a symbol (.). The source command is usually used to re-execute the newly modified initial or file so that it can be immediately Take effect without having to log out and log back in.

/bin/bash test.sh

If you use the bash method, although you can run test.sh to configure environment variables, the bash command creates a child process, and the variables defined by it will not be passed to the parent process, so you cannot actually modify the running environment variables after execution.

Delete environment variables

  1. Use the readonly command to set read-only variables. If the readonly command is used, the variables cannot be modified or cleared.

  2. Use the unset command to clear environment variables

unset TEMP_KEVIN 
#删除环境变量TEMP_KEVIN

2. View environment variables

  1. The env command is the abbreviation of environment and is used to list all environment variables
    env

  2. The software directory contained in the system PATH path
    echo $PATH
    adds the software running path to the path.

  3. Display environment variable HOME
    echo $HOME
    /home/terry

  4. Use C programs to access and set environment variables
    For users of C programs, you can use the following three functions to set or access an environment variable.

  • getenv() accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string. If the accessed environment variable does not exist, NULL will be returned.

  • setenv() is a function that sets an environment variable in the program.

  • unsetenv() function clears a specific environment variable.


3. Examples of environment variable configuration

Take configuring the environment variables of Android-NDK as an example:

  1. First download the corresponding ndk
wget -c http://dl.google.com/android/ndk/android-ndk64-r10b-linux-x86_64.tar.bz2
sudo gedit ~/.bashrc
#在文件末尾添加以下内容:
export   NDK=/文件夹路径 
export   PATH=${
    
    PATH}:$NDK

The folder path must be accurate to the highest level path after decompression, for example: /home/yngzmiao/android-ndk-r10b
Save the file and make it effective:

source  ~/.bashrc

After installing and configuring Android-NDK, you need to perform installation verification to confirm correct installation and configuration:

ndk-build

Guess you like

Origin blog.csdn.net/u011795345/article/details/126130277