How to Manage Environment Variables on Arch Linux

How to Manage Environment Variables on Arch Linux

Environment variables are a foundational concept in Linux systems, including Arch Linux. They play a critical role in how the system and applications behave, allowing users to customize their environments, manage configuration settings, and streamline command-line workflows. This article will walk you through what environment variables are, why they matter, and how to effectively manage them on an Arch Linux system.


What Are Environment Variables?

Environment variables are dynamic values that affect the processes and behavior of running applications on a Linux system. These variables are inherited by child processes and are often used to pass configuration data to software applications without hardcoding them.

Common examples include:

  • PATH: Directories the shell looks in for executable files.
  • HOME: Path to the current user’s home directory.
  • EDITOR: Default text editor for command-line operations.
  • LANG: Language and locale settings.

Environment variables can be system-wide or user-specific, temporary (for a session), or persistent (across reboots).


Viewing Current Environment Variables

To view all currently defined environment variables, you can use the following commands:

printenv

Or to view a specific variable:

echo $VARIABLE_NAME
# Example:
echo $HOME

To list all shell variables, including functions and internal shell variables, use:

set

For a more readable and paged output:

env | less

Setting Temporary Environment Variables

You can create a temporary environment variable in the shell session like this:

export MYVAR="Hello World"

This variable is now accessible in the current terminal session and any subprocesses spawned from it:

echo $MYVAR
# Output: Hello World

However, once the session is closed, the variable will be lost. This is useful for quick, session-specific changes.


Making Environment Variables Persistent

To persist environment variables across sessions, you must define them in appropriate configuration files. Depending on the use case (user-specific or system-wide), different files are used.


1. User-Specific Variables

These are variables available only to a specific user.

a. ~/.bashrc (or ~/.zshrc if you’re using Zsh)

This file is used for variables that are needed in interactive shell sessions.

# Add at the end of ~/.bashrc
export EDITOR=nano
export PATH="$PATH:$HOME/scripts"

Then apply the changes:

source ~/.bashrc

b. ~/.profile or ~/.bash_profile

These are executed at login. Useful for variables that should be available even in non-interactive shell sessions or graphical environments.

# Add at the end of ~/.profile
export BROWSER=firefox

Don’t forget to reload or re-login:

source ~/.profile

Tip: Use .profile for login shells, .bashrc for interactive ones.


2. System-Wide Variables

For variables that should be available to all users.

a. /etc/environment

This is the most neutral place to set system-wide environment variables. It is a simple key-value file, so you don’t use export here.

# Example contents of /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
EDITOR="vim"
LANG="en_US.UTF-8"

Edit with a text editor:

sudo nano /etc/environment

After modifying /etc/environment, a reboot or re-login is necessary for the changes to take effect.

b. /etc/profile or /etc/profile.d/*.sh

  • /etc/profile is a shell script executed at login.
  • Files in /etc/profile.d/ are sourced by /etc/profile.

To define a system-wide environment variable:

sudo nano /etc/profile.d/myenv.sh

And inside:

export NODE_ENV=production

Make sure the file is executable:

sudo chmod +x /etc/profile.d/myenv.sh

This allows more modular configuration than editing /etc/profile directly.


Managing Environment Variables in Desktop Environments

If you’re running a graphical environment like GNOME, KDE, or Xfce, variables set in shell configuration files might not apply to GUI applications. Here’s how to handle them properly.

1. Using .xprofile or .xinitrc

For display managers (like LightDM, GDM, etc.), use ~/.xprofile to set variables available to your X session:

# ~/.xprofile
export GTK_THEME="Adwaita-dark"
export QT_QPA_PLATFORMTHEME="qt5ct"

If you’re launching X manually with startx, use ~/.xinitrc:

export GTK_IM_MODULE=ibus
exec startplasma-x11  # Or your DE of choice

Note: Don’t forget the exec line; it starts your window manager or desktop environment.


2. Environment Variables for Systemd User Services

Arch Linux uses systemd extensively. To pass environment variables to user systemd services, use:

systemctl --user edit your-service-name.service

Add under [Service]:

[Service]
Environment="ENV_VAR=value"

Reload the daemon and restart the service:

systemctl --user daemon-reexec
systemctl --user restart your-service-name.service

Unsetting Environment Variables

To remove a temporary environment variable from your session:

unset VARIABLE_NAME

For example:

unset MYVAR

This only affects the current session and won’t persist after a reboot or logout.


Debugging Environment Variable Issues

Sometimes variables don’t get applied as expected. Here are a few steps to debug:

  1. Check where the variable is set: Is it in .bashrc, .profile, or /etc/environment?
  2. Use printenv or env to confirm if the variable is exported.
  3. Re-login or restart to make sure login scripts are sourced.
  4. Check if your display manager sources .profile — not all do. In that case, use .xprofile or systemd user services.
  5. For scripts, ensure #!/bin/bash is used and not #!/bin/sh if you’re relying on Bash-specific behavior.

Best Practices for Managing Environment Variables on Arch Linux

  • Use the right file for the context: Shell session? Use .bashrc. GUI apps? Use .xprofile or systemd user units.
  • Don’t overload PATH unnecessarily — use scripts or functions if logic is involved.
  • Avoid setting sensitive information in plaintext in readable files; consider secret managers or encrypted storage.
  • Document your changes in config files with comments, especially in /etc/profile.d/ or .profile.

Conclusion

Managing environment variables effectively can greatly improve your productivity, streamline your system configuration, and provide better control over how applications run. On Arch Linux, with its do-it-yourself philosophy, understanding and correctly applying environment variables is essential for both everyday use and system administration tasks.

Whether you’re a casual user trying to change your default editor or a power user setting up development environments, knowing how to view, set, persist, and manage environment variables will give you the flexibility and control expected from a powerful Linux system like Arch.

If you stick to the principles outlined above and make thoughtful choices about where and how to set your variables, you’ll avoid common pitfalls and maintain a clean, efficient environment.