What are the methods of configuring environment variables in a Linux environment?
In a Linux environment, there are several ways to configure environment variables:
- Set environment variables in the user’s personal profile: Environment variables can be set in the user’s personal profile (such as ~/.bashrc or ~/.bash_profile) using the export command. For example: export PATH=$PATH:/path/to/directory.
- Setting environment variables in system-level configuration files: Using the export command in system-level configuration files (such as /etc/profile or /etc/environment) allows for setting environment variables that are universally effective for all users.
- Set environment variables directly using the export command: You can use the export command in the command line to set temporary environment variables. For example, export PATH=$PATH:/path/to/directory.
- To set environment variables in a Shell script, you can use the export command. For instance, you can set the PATH variable by using: export PATH=$PATH:/path/to/directory. Then, before running the script, use the source command to load the environment variables in the script.
- You can set environment variables directly in the /etc/environment file. Each line in the file should be in the format key=value. For example, you can set the PATH variable like this: PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”.
No matter which method you use, the environment variables will only take effect in the current Shell session. If you want the environment variables to take effect every time you log in, you can add them to the corresponding configuration file.