If you want to set a variable in your shell (as opposed to the superuser's):

  1. Make sure your .bashrc exists
ls -A ~/.bashrc
  1. Add your variable at the bottom
export VARIABLE=value
  1. Save, then open a new terminal and verify the variable is set
$ echo $VARIABLE
value

Keep in mind this depends on your shell.

Answer from marcdahan on Stack Overflow
๐ŸŒ
InterServer
interserver.net โ€บ home โ€บ linux โ€บ how to set environment variables permanently in linux
How to Set Environment Variables Permanently in Linux - Interserver Tips
January 2, 2026 - You can set environment variables permanently in several ways: For just your user account: Edit your shell configuration file (like .bashrc, .bash_profile, or .zshrc). For all users: Edit the global /etc/environment file or /etc/profile. For system services: Use systemd service overrides.
Discussions

How to permanently set environmental variables - Unix & Linux Stack Exchange
Then you don't need to set LD_LIBRARY_PATH, see also here. ... Careful, there is more to this story than initially appears. I invite you to check my answer. ... You can add it to the file .profile or your login shell profile file (located in your home directory). To change the environmental variable "permanently... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
but as it turns out this only sets an env variable in a particular shell process and when I open other shell and run printenv I no longer see those environment variables that I set. Yes - thats how it works. if you want to set things for each new bash shell, you can do that in your bash configs. particular shell process Exported variables set up a COPY of the variable to the CHILDREN processes. Thats basically IT.. one way, and the children do not alter each others variables. https://www.reddit.com/r/linux4noobs/comments/1cyzrqb/when_setting_the_environment_variables_on_the/ https://ioflood.com/blog/bash-environment-variables/ More on reddit.com
๐ŸŒ r/linux4noobs
2
2
May 24, 2024
Would someone explain the environment variables like I'm a crayon eating five?
Think of environment variables like parameters that are automatically passed to every program you run. For example, instead of man --language=italian foo to get an Italian man page, you can have an environment variable that says your language is Italian (in actuality this would be the variable LANG=it_IT.utf8). Many environment variables (like the above LANG) are standardized, so that different applications can all use the same variable. And programs can ignore the ones they don't care about. Another more advanced use case is for securely passing parameters to programs. When you run foo --mypassword=abcd1234, anyone can run ps and see the arguments, and thus see your password. However if you set the password in an environment variable, only you can see it. Must know bit: How to set them is probably all there is to really know. When you set a variable in your shell by doing foo=bar, this isn't creating an environment variable. It's just create a variable (no "environment" part). To make it an environment variable, you have to export it. Such as by either doing two commands foo=bar; export foo or a single command export foo=bar. You can also set an environment variable for just a single instance of a program, and not have it persist. For example instead of doing: export foo=bar myprogram unset foo ... you can do foo=bar myprogram Lastly, when you set an environment variable, it doesn't live beyond the lifetime of the shell. So if you close your terminal and open a new one, the variable will be gone. If you want it to always be there, even when you launch a new shell, you'll need to set it in a rc/profile script (e.g. ~/.bashrc). More on reddit.com
๐ŸŒ r/linuxquestions
4
19
November 17, 2021
Linux needs an intuitive way to set global environmental variables (AKA getting touchscreen gestures enabled in Firefox is way too difficult)
No need to mess with PAM just for environment variables. There is /etc/environment which you can use exactly for this. Simple syntax, just the variable, bam, done. Hasn't changed in a long time, as far as I know, like on Windows. Of course, though, that's owned by root because it affects the system and all users. Who else should it be owned by? For user-specific stuff, just throw something like export MOZ_ENABLE_WAYLAND=1 into your ~/.profile or something which should work for GUI apps too on most distributions. More on reddit.com
๐ŸŒ r/linux
124
124
November 24, 2022
๐ŸŒ
Linux Mint Forums
forums.linuxmint.com โ€บ board index โ€บ main edition support โ€บ beginner questions
Set environment variables permanently [SOLVED] - Linux Mint Forums
May 27, 2018 - Version: Linux Mint 22.2, 64-bit; Cinnamon 4.6.6; Machine: ASUSTek, PRIME Z270-A, Quad Core, Intel Core i7-7700 GPU: NVIDIA GP106 [GeForce GTX 1060 6GB]; total drive space: 5.5 TB (2 SSDs + 1 internal HD + 1 external HD); UEFI dual boot w/ W10 ... Hello, mrodent. $HOME/.bashrc is a perfectly acceptable place to define your user specific environment variables...
Top answer
1 of 4
385

You can add it to the file .profile or your login shell profile file (located in your home directory).

To change the environmental variable "permanently" you'll need to consider at least these situations:

  1. Login/Non-login shell
  2. Interactive/Non-interactive shell

bash

  1. Bash as login shell will load /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile in the order
  2. Bash as non-login interactive shell will load ~/.bashrc
  3. Bash as non-login non-interactive shell will load the configuration specified in environment variable $BASH_ENV
$EDITOR ~/.profile
#add lines at the bottom of the file:  
     export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

zsh

$EDITOR ~/.zprofile
#add lines at the bottom of the file:  
     export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

fish

set -Ux LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
set -Ux ORACLE_HOME /usr/lib/oracle/11.2/client64

ksh

$EDITOR ~/.profile
#add lines at the bottom of the file:  
     export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

bourne

$EDITOR ~/.profile
#add lines at the bottom of the file:  
     LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib     
     ORACLE_HOME=/usr/lib/oracle/11.2/client64
     export LD_LIBRARY_PATH ORACLE_HOME

csh or tcsh

$EDITOR ~/.login
#add lines at the bottom of the file:  
     setenv LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
     setenv ORACLE_HOME /usr/lib/oracle/11.2/client64

If you want to make it permanent for all users, you can edit the corresponding files under /etc/, i.e. /etc/profile for Bourne-like shells, /etc/csh.login for (t)csh, and /etc/zsh/zprofile and /etc/zsh/zshrc for zsh.

Another option is to use /etc/environment, which on Linux systems is read by the PAM module pam_env and supports only simple assignments, not shell-style expansions. (See Debian's guide on this.)

These files are likely to already contain some assignments, so follow the syntax you see already present in your file.

Make sure to restart the shell and relogin the user, to apply the changes.

If you need to add system wide environment variable, there's now /etc/profile.d folder that contains sh script to initialize variable.
You could place your sh script with all you exported variables here.
Be carefull though this should not be use as a standard way of adding variable to env on Debian.

2 of 4
102

To do if for all users/shells, depending on distro you could use /etc/environment or /etc/profile. Creating a new file in /etc/profile.d may be preferable if it exists, as it will be less likely to conflict with updates made by the packaging system.

In /etc/environment, variables are usually set with name=value, eg:

ORACLE_HOME=/usr/lib/oracle/11.2/client64

In /etc/profile, you must use export since this is a script, eg:

export ORACLE_HOME=/usr/lib/oracle/11.2/client64

Same goes for a file under /etc/profile.d, there also may be naming restrictions which must be met for the file to work. On Debian, the file must have the extension .sh (although does not need a bang line or executable permissions since it is sourced). check your distro documentation or look at the /etc/profile script to see how these files are loaded.

Note also though that setting LD_LIBRARY_PATH permanently is potentially problematic, including being a security risk. As an alternative, I would suggest finding some way to prepend the LD_LIBRARY_PATH to the start of the command line for each program that needs it before running. Eg:

LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib myprog

One way to do this is to use a wrapper script to run the program. You could give this the same name as your program and put it in /usr/local/bin or anywhere that appears before the location of your program in PATH. Here is an example script (don't forget to chmod +x the script):

#!/bin/sh
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib /real/location/of/myprog "$@"
๐ŸŒ
DigiCert
docs.digicert.com โ€บ en โ€บ digicert-keylocker โ€บ overview โ€บ secure-credentials โ€บ set-up-secure-credentials-for-linux โ€บ persistent-environment-variables-for-linux.html
Persistent environment variables for Linux
When your API key and client certificate password are securely stored in the properties file or Linux Pass, remove them from persistent variables. This action prevents unauthorized access and keeps your credentials secure. Launch the Terminal application. ... export SM_API_KEY= <API key> export SM_CLIENT_CERT_PASSWORD=<P12 client authentication certificate password> Select CTRL+X to exit. Select Y to save. Select Enter to keep the same file name. Run the new .profile by restarting Terminal or using the following command: ... Configure the HTTPS_PROXY environment variable if the client tool need to communicate through a proxy.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/linux4noobs โ€บ how to set environment variables globally and permanently so they can be available in all shell processes i open and not just single one ?
r/linux4noobs on Reddit: How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
May 24, 2024 -

Hello. I am still noob in Linux ecosystem. I wanted to set some environment variables and ran command export VAR=value , but as it turns out this only sets an env variable in a particular shell process and when I open other shell and run printenv I no longer see those environment variables that I set.

How can I set env variables that can be used in all shells ? Should I set it in /etc/profile.d/ directory or is there some other location I should write my env variables to ? If this is not the right directory where should I define the env variables ?

Top answer
1 of 2
2
but as it turns out this only sets an env variable in a particular shell process and when I open other shell and run printenv I no longer see those environment variables that I set. Yes - thats how it works. if you want to set things for each new bash shell, you can do that in your bash configs. particular shell process Exported variables set up a COPY of the variable to the CHILDREN processes. Thats basically IT.. one way, and the children do not alter each others variables. https://www.reddit.com/r/linux4noobs/comments/1cyzrqb/when_setting_the_environment_variables_on_the/ https://ioflood.com/blog/bash-environment-variables/
2 of 2
2
That may depend on the distribution and the types of shell you are using. I don't have one here that uses an /env directory on the root file system. On Debian, Ubuntu and a lot of other systems you have /etc/profile to setup stuff systemwide and for all users (valid for sh, ash, ksh, bash, zsh). csh would be different but this generally not used by most Linux users. Fish might be different, too, I do not use it. NixOS has a different structure. General stuff might go into /etc/nix/nix.conf If it is only for one user in his/her home directory: there is $HOME/.profile to set this up (also sh, ash, ksh, bash, zsh). Concerning containers: you have to set this up per container. This is also valid if you are using toolbox inside an "immutable" or "atomic" distribution like Fedora Silverblue. By the way, instead of predefining specific shell variables globally, you may create shell scripts for all users residing in /usr/local/bin that contains the shell variables needed. E.g. /usr/local/bin/linuxoracle.sh: #!/usr/bin/env bash # file: linuxoracle.sh HOST=$(hostname) echo "$(shuf -i 2020-2030 -n 1) is the Year of the Linux Desktop on $HOST"
๐ŸŒ
Google
aboutchromebooks.com โ€บ linux-set-environment-variable
How to Set an Environment Variable in Linux
January 8, 2026 - The variable should display for all user accounts. Add the export command to your .bashrc file in your home directory, then run source ~/.bashrc to apply changes immediately. .bashrc runs for interactive non-login shells, while .bash_profile runs only for login shells. Use .bashrc for most environment variables.
๐ŸŒ
Softhints
softhints.com โ€บ how-to-set-environment-variables-permanently-in-linux-mint
How to Set environment variables permanently in Linux Mint
February 8, 2021 - The session environment variable is created by writing in terminal: ... Variable RSTUDIO_PANDOC is used for RMarkdown. To use RMarkdown generation outside Rstudio you need to have it set. For example PyCharm you can open the console and run the command above. If the RMarkdown works properly you can move to the next step and set it permanently.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to set an environment variable and make it persistent in Linux - YouTube
This chennal helps to all the folks from biggeners to experience to learn corporate and technical work and on what stuff most of the IT industies take work f...
Published ย  August 7, 2024
๐ŸŒ
Void Linux
voidlinux.org
Void Linux
libxbps: fix XBPS_ARCH environment variable if architecture is also defined in a configuration file. duncaen ... libxbps: fix how the automatic/manual mode is set when replacing a package using replaces. This makes it possible to correctly replace manually installed packages using a transitional ...
Top answer
1 of 2
3

Environments for shells

Essentially, anything that runs processes will tend to read a configuration file on starting up, and to affect the environment of that, you need to hit its configuration file.

For user shells, "obvious" places are .profile, .bashrc, .bash_profile (I think) and maybe a couple of others I don't remember. Obviously, more and others if you use zsh, csh, tcsh or whatever as a shell.

There are initialization files read by your windowing environment, which may be either KDE or Gnome. The particular window manager you run underneath that may also read a config file. I admit I don't know the names of those files even for my own installation.

Finally, there are usually "master" configuration files for all those environments somewhere in /etc. They provide defaults for stuff the users don't.

I think that programs that install themselves conscientiously check the various possibilities. Various Linux distributions may offer some helper scripts for this.

cron

This one is a lot easier. For security reasons, cron only passes a couple of environment variables to subprocesses, ALWAYS. I think USER is one of those, and MAILTO another. As far as I know, there's no PATH set - this often annoys newbies. The environment of a cron job is completely different from your shell environment! Anything you want in the environment, you either pass in on the command line in crontab, or you start up a script and let that set up whatever environment it needs.

2 of 2
3

To apply a bashrc change to all users, you can modify /etc/bash.bashrc (This is for Ubuntu).

Also, as indicated in the answer above, make sure that this file is sourced by /etc/profile.

๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ windows โ€บ wsl โ€บ wsl-config
Advanced settings configuration in WSL | Microsoft Learn
April 15, 2026 - Configure local settings with wsl.conf per-distribution for each Linux distribution running on WSL 1 or WSL 2.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ sysadmin โ€บ environment variables in linux: how to list, set, and manage
Environment Variables in Linux: How to List, Set, and Manage
December 10, 2025 - To permanently remove a variable you stored in a configuration file, open the file and remove the line containing the variable definition. ... This article showed how to set and unset environmental variables on a Linux system.
๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ linux-newbie-8 โ€บ how-to-set-environment-variables-that-i-use-everyday-permanently-819603
how to set environment variables that i use everyday permanently
July 13, 2010 - Hi All, I have bunch of environment variables that i have to set always for my work.Someone mentioned i can write a script to dp this and i googled it
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ remote โ€บ troubleshooting
Remote Development Tips and Tricks
November 3, 2021 - If you need to configure the startup environment, you can use the environment setup script as described here. The environment for the remote extension host and terminal are based on the default shell's configuration scripts. To evaluate the environment variables for the remote extension host process, the server creates an instance of the default shell as an interactive login shell.
๐ŸŒ
HackerNoon
hackernoon.com โ€บ mastering-environment-variables-in-linux-a-comprehensive-guide
Mastering Environment Variables in Linux: A Comprehensive Guide | HackerNoon
November 22, 2023 - Comprehensive guide to setting environment variables in Linux for single users and system-wide configurations, including code examples and detailed explanations
๐ŸŒ
devconnected
devconnected.com โ€บ home โ€บ linux system administration โ€บ how to set and unset environment variables on linux
How To Set and Unset Environment Variables on Linux โ€“ devconnected
October 19, 2019 - environment variablelinux administrationlinux set environment variablelinux set pathlinux system administration ... [โ€ฆ] a graphical application remotely, you will get an error message stating that you have no DISPLAY environment variable [โ€ฆ] Reply ยท Writing Scripts on Linux using Bash โ€“ devconnected December 1, 2019 - 11:05 pm ยท [โ€ฆ] : if you want to make your changes permanent, follow those steps to update your PATH variable [โ€ฆ] Reply