linux set global variable

Environment variables in Linux include system-level and user-level. System-level environment variables are system variables that every user who logs in to the system must read, while user-level environment variables are environment variables that are loaded when the user uses the system. Therefore, the files for managing environment variables are also divided into system-level and user-level. Below is a relatively clear introduction to the files found on the Internet (slightly modified) [1]:
1. System-level:

(1) /etc/profile: This file is the first file used by the operating system to customize the user environment when a user logs in, and applies to every user logging in to the system. This file is generally called /etc/bash.bashrc file. /etc/bash.bashrc: The system-level bashrc file.

  (2)/etc/profile.d/

          Each script in the /etc/profile.d/ directory will be executed by /etc/profile, so for user-defined global variables, generally create a new script file in the /etc/profile. The variables or initialization settings you want to define are written in this script, then after the system starts, they will be executed automatically and are visible to all users. E.g:

     Each user in redhat must specify their own environment variables, which will be more troublesome, so how to configure an environment variable that all users can use?

The following is an example of the installation of vcs

vi   /etc/profile.d/vcs.sh

Enter the following in the new java.sh:

 

#set java environment

VCS_HOME=/home/eda/synopsys/vcs-2014.03
PATH=$VCS_HOME/bin:$PATH
export VCS_HOME PATH

Save and exit, then assign permissions to vcs.sh: chmod 755 /etc/profile.d/vcs.sh

       

           Difference between /etc/profile and /etc/profile.d in Linux environment

     the difference:
1. Both files are used to set environment variable files. /etc/profile is a permanent environment variable and a global variable. /etc/profile.d/ sets all users to take effect.
2. /etc/profile.d/ is easier to maintain than /etc/profile. If you don’t want any variables, just delete the corresponding shell script under /etc/profile.d/. You don’t need to change this file like /etc/profile.

 

(2) /etc/environment: The second file used by the operating system when logging in. The system sets the environment variables of the environment file before reading your own profile.


2. User level (these files are in the home directory):

(1) ~/.profile: Each user can use this file to enter shell information dedicated to their own use. When the user logs in, the file is executed only once! By default, he sets some environment variables and executes the user's . bashrc file. Here is the recommended place to put personal settings

(2) ~/.bashrc: This file contains bash information specific to your bash shell, which is read when you log in and every time you open a new shell. It is not recommended to put it here, because every time a shell is opened, this file will be read once , which will definitely affect the efficiency. ~/.bash_profile or ~./bash_login

Usage: Some programs we want to use in any path, and it is very troublesome to enter the absolute path every time, so you can add a directory to the environment variable, and then put the program to be run in this directory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324755552&siteId=291194637