亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
View Current Environment Variables
Set Environment Variables Temporarily
Make Environment Variables Persistent
Remove Environment Variables
Best Practices
Home System Tutorial LINUX How to Manage Environment Variables in Linux

How to Manage Environment Variables in Linux

Aug 03, 2025 am 07:00 AM
linux environment variables

To 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.

How to Manage Environment Variables in Linux

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.

How to Manage Environment Variables in Linux

View Current Environment Variables

To see all currently set environment variables, use the printenv or env command:

printenv

Or to check a specific variable:

How to Manage Environment Variables in Linux
printenv HOME
# or
echo $HOME

Common variables include:

  • HOME: User’s home directory
  • PATH: Directories where the system looks for executable programs
  • USER: Current user
  • SHELL: Default shell

Using set shows all shell variables, including environment variables and functions (more verbose than printenv).

How to Manage Environment Variables in Linux

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to add a new disk to Linux How to add a new disk to Linux Jun 27, 2025 am 12:15 AM

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.

Fixed the failure to upload files in Windows Google Chrome Fixed the failure to upload files in Windows Google Chrome Jul 08, 2025 pm 02:33 PM

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

What is the sudo command and when should I use it? What is the sudo command and when should I use it? Jul 02, 2025 am 12:20 AM

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.

How to manage groups on Linux How to manage groups on Linux Jul 06, 2025 am 12:02 AM

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.

How do you specify environment variables in a Docker container? How do you specify environment variables in a Docker container? Jun 28, 2025 am 12:22 AM

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

How to find my private and public IP address in Linux? How to find my private and public IP address in Linux? Jul 09, 2025 am 12:37 AM

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.

How do I get environment variables using the os.Getenv() function in Go? How do I get environment variables using the os.Getenv() function in Go? Jun 27, 2025 am 02:04 AM

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

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

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

See all articles