How to Manage Environment Variables in Linux
Aug 03, 2025 am 07:00 AMTo manage environment variables in Linux, view them using printenv or echo $VAR, set temporary ones with export VAR="value", make them persistent by adding export commands to shell files like ~/.bashrc or system-wide files like /etc/environment, and remove them with unset VAR; always follow best practices such as avoiding sensitive data in scripts and keeping PATH clean.
Managing environment variables in Linux is essential for configuring system behavior, running scripts, and setting up development environments. These variables store values that processes and applications use during execution. Here's how to effectively manage them.

View Current Environment Variables
To see all currently set environment variables, use the printenv
or env
command:
printenv
Or to check a specific variable:

printenv HOME # or echo $HOME
Common variables include:
HOME
: User’s home directoryPATH
: Directories where the system looks for executable programsUSER
: Current userSHELL
: Default shell
Using set
shows all shell variables, including environment variables and functions (more verbose than printenv
).

Set Environment Variables Temporarily
You can define a variable for the current shell session:
export MY_VAR="Hello World"
The export
command makes the variable available to child processes. Without export
, it's only available in the current shell.
To verify:
printenv MY_VAR
This variable will be lost when the shell session ends.
? Tip: You can also set a variable for a single command without exporting:
MY_VAR="test" ./script.sh
Make Environment Variables Persistent
Temporary variables vanish after logout. To make them permanent, add them to shell configuration files.
For bash, edit one of these files depending on scope:
- User-specific:
~/.bashrc
,~/.bash_profile
, or~/.profile
- System-wide:
/etc/environment
or/etc/profile.d/custom.sh
Example: Add to ~/.bashrc
export API_KEY="your-key-here" export PATH="$PATH:/opt/myapp/bin"
Then reload the file:
source ~/.bashrc
For system-wide settings in /etc/environment
, use this format (no export
):
MY_VAR="system-wide value"
?? Be cautious editing system-wide files — a syntax error can affect all users.
Remove Environment Variables
To unset a variable in the current session:
unset MY_VAR
This removes it completely. After this, printenv MY_VAR
returns nothing.
Best Practices
- Avoid putting sensitive data (like passwords) directly in
.bashrc
. Use secure credential managers or.env
files with proper permissions. - Use
/etc/profile.d/*.sh
for system-wide scripts — clean and modular. - Always
source
config files after editing, or start a new shell to test. - Keep
PATH
clean — avoid duplicate or invalid directories.
Managing environment variables in Linux isn’t complex, but doing it right ensures your system and apps behave as expected. Whether temporary or permanent, knowing where and how to set them gives you full control over your environment.
The above is the detailed content of How to Manage Environment Variables in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Have problems uploading files in Google Chrome? This may be annoying, right? Whether you are attaching documents to emails, sharing images on social media, or submitting important files for work or school, a smooth file upload process is crucial. So, it can be frustrating if your file uploads continue to fail in Chrome on Windows PC. If you're not ready to give up your favorite browser, here are some tips for fixes that can't upload files on Windows Google Chrome 1. Start with Universal Repair Before we learn about any advanced troubleshooting tips, it's best to try some of the basic solutions mentioned below. Troubleshooting Internet connection issues: Internet connection

sudo stands for "substituteuserdo" or "superuserdo", allowing users to run commands with permissions of other users (usually root). Its core uses include: 1. Perform system-level operations such as installing software or editing system files; 2. Accessing protected directories or logs; 3. Manage services such as restarting nginx; 4. Modify global settings such as /etc/hosts. When using it, the system will check the /etc/sudoers configuration and verify the user password, provide temporary permissions instead of continuously logging in as root, ensuring security. Best practices include: only when necessary, avoid blindly executing network commands, editing sudoers files with visudo, and considering continuous operations.

To manage Linux user groups, you need to master the operation of viewing, creating, deleting, modifying, and user attribute adjustment. To view user group information, you can use cat/etc/group or getentgroup, use groups [username] or id [username] to view the group to which the user belongs; use groupadd to create a group, and use groupdel to specify the GID; use groupdel to delete empty groups; use usermod-aG to add users to the group, and use usermod-g to modify the main group; use usermod-g to remove users from the group by editing /etc/group or using the vigr command; use groupmod-n (change name) or groupmod-g (change GID) to modify group properties, and remember to update the permissions of relevant files.

There are three common ways to set environment variables in a Docker container: use the -e flag, define ENV instructions in a Dockerfile, or manage them through DockerCompose. 1. Adding the -e flag when using dockerrun can directly pass variables, which is suitable for temporary testing or CI/CD integration; 2. Using ENV in Dockerfile to set default values, which is suitable for fixed variables that are not often changed, but is not suitable for distinguishing different environment configurations; 3. DockerCompose can define variables through environment blocks or .env files, which is more conducive to development collaboration and configuration separation, and supports variable replacement. Choose the right method according to project needs or use multiple methods in combination

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

TogetenvironmentvariablesinGo,useos.Getenv(),butconsiderLookupEnvforexistencechecks.1.Useos.Getenv("VAR_NAME")toretrieveavariable’svalueasastring,returningemptyifunset.2.Useos.LookupEnv()todistinguishbetweenunsetandemptyvariables.3.Provided

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach
