• In Linux, we often need to set environment variables. Here are some common ways to set environment variables.

Reading environment variables

  1. Export: Displays all environment variables defined by the current system
  2. Echo $PATH: Outputs the value of the current PATH environment variable

Set the current terminal of the environment variable: export PATH

  1. export PATH=/you/bin/path:$PATH

Ps: Do not forget to add the original configuration, that is, the $PATH part of the environment variable to avoid overwriting the original configuration.

  • Effective time: Immediately
  • Validity period: Valid on the current terminal but invalid after the window is closed
  • Valid range: This parameter is valid only for the current user

Set the current user of the environment variable: vim ~/.bashrc, vim ~/.bash_profile

Modify the ** /. Bashrc or /. Bash_profile ** file in the user directory to configure:

  1. Bashrc, add export PATH=/you/bin/ PATH :$PATH to the last line of the file

. 2. Vim ~/. Bash_profile add export PATH=/you/bin/ PATH :$PATH to the last line of the file.

  • Bashrc and source ~/. Bash_profile take effect immediately after the source ~/. Bashrc and source ~/
  • Validity period: Permanent
  • Valid range: This parameter is valid only for the current user

Set environment variables for all users: vim /etc/bashrc, vim /etc/profile, vim /etc/environment

Modifying the system configuration file requires the administrator permission (such as root) or the user’s write permission to the system configuration file:

  1. Vim /etc/bashrc add export PATH=/you/bin/ PATH :$PATH to the last line of the file

. 2. Vim /etc/profile add export PATH=/you/bin/path: PATH∗∗ to the last line of the file. ∗∗vim/etc/environment∗∗. Add ∗∗exportPATH=/you/bin/path: path ** to the last line of the file. 3. **vim /etc/environment**, add **export PATH=/you/bin/ PATH :PATH∗∗ to the last line of the file. ∗∗vim/etc/environment∗∗. Add ∗∗exportPATH=/you/bin/path: path to the last line of the file.

Ps: If the system configuration file cannot be edited, modify its properties, for example, chmod -v u+w /etc/bashrc.

  • Setting time: The setting takes effect on a newly opened terminal or manually using source /etc/bashrc, source /etc/profile, and source /etc/environment
  • Validity period: Permanent
  • Validity range: This parameter is valid for all users

tip

  • You can define a variable profile file xxxx.profile under the XXXX project, in which you define a set of variables using export, followed by the ~/.bash_profile file: Source XXXX.profile, so that each login can use a custom set of variables in the Shell script.

  • Use the alias command to define the alias of some commands, such as alias rm=”rm -i”, and then add this code to ~/. Bash_profile. This will be the same as using rm -i every time.