Several ways to modify environment variables in ubuntu

Several ways to modify environment variables in ubuntu

There are various ways to modify the environment variables of an Ubuntu system, including:

  1. Temporarily modify environment variables: Use the export command in the terminal to temporarily modify environment variables. For example, to add the PATH environment variable to a new directory, you can run the following command:
export PATH=$PATH:/path/to/new/directory

This will add the new directory to the PATH environment variable in the current terminal session. However, these changes will be removed when the terminal is closed.

  1. Permanently modify environment variables: To permanently modify environment variables, you can edit system-level configuration files or user-level configuration files. System-level configuration files include /etc/environment and /etc/profile files, while user-level configuration files include ~/.bashrc and ~/.profile files.
  • Adding an environment variable to the /etc/environment file will make it available to all users and processes. For example, to add a new directory to the PATH environment variable, edit the /etc/environment file and add the following line:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/path/to/new/directory"
  • Adding an environment variable in /etc/profile will make it available to all users. For example, to add a new directory to the PATH environment variable, edit the /etc/profile file and add the following line:
export PATH=$PATH:/path/to/new/directory
  • Adding an environment variable in the ~/.bashrc file will make it available to the current user. For example, to add a new directory to the PATH environment variable, edit the ~/.bashrc file and add the following line:
export PATH=$PATH:/path/to/new/directory
  • Adding an environment variable to the ~/.profile file will make it available to the current user. For example, to add a new directory to the PATH environment variable, edit the ~/.profile file and add the following line:
export PATH=$PATH:/path/to/new/directory
  1. Difference: Different methods have different scope and permanence.
  • Temporarily modifying environment variables is only valid in the current terminal session and is deleted after closing the terminal.
  • Adding an environment variable to the /etc/environment file will make it available to all users and processes and persist across system reboots.
  • Adding an environment variable to the /etc/profile file will make it available to all users and persist across system reboots.
  • Adding an environment variable to the ~/.bashrc file will make it available to the current user and persist after the user logs out.
  • Adding an environment variable to the ~/.profile file will make it available to the current user and persist after the user logs out.

Guess you like

Origin blog.csdn.net/qq_43577613/article/details/130875690