360

My variables are

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

How to save these variables permanently ?

Graeme
  • 34,027
user3021349
  • 16,569

4 Answers4

374

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.

ekl1pse
  • 101
Kiwy
  • 9,534
  • This .profile in /etc/ but I don't how to set the variables in this .profile please tell me – user3021349 Feb 28 '14 at 13:44
  • 7
    .profile is in your home directory not /etc/ – Kiwy Feb 28 '14 at 13:45
  • How to check this .profile in my home directory?? – user3021349 Feb 28 '14 at 13:47
  • 1
    [Admin@localhost etc]$ cat ~/.profile cat: /home/Admin/.profile: No such file or directory [Admin@localhost etc]$ – user3021349 Feb 28 '14 at 13:49
  • what is your shell ? – Kiwy Feb 28 '14 at 13:50
  • I'm using bash shell – user3021349 Feb 28 '14 at 13:53
  • 6
    @user3021349 I don't meant to be rude but if you think one second you can also use a different editor you master. :wq is the command to write file and exit in vi don't forget to type esc before – Kiwy Feb 28 '14 at 13:59
  • sorry!! works fine .. Thank you foe efforts while answering my question and thank you for answering my question – user3021349 Feb 28 '14 at 14:01
  • Parts of this are grossly wrong: Bourne shell doesn’t allow export x=y but needs x=y; export x; the C shell uses setenv. – mirabilos Feb 28 '14 at 14:06
  • @mirabilos thanks, i proceed to an edit :-) – Kiwy Feb 28 '14 at 14:14
  • @Kiwy ok. Do note that export x=y works fine in the Korn shell, and that .profile is only read by login shells (some Korn shell variants use .kshrc or .mkshrc for interactive nōn-login shells). I’ll fix that for you. – mirabilos Feb 28 '14 at 14:18
  • 5
    You'll need to consider the environment variables in crontab scripts. None of these locations will be looked up when a crontab script is running. – yegle Mar 01 '14 at 01:04
  • @yegle as I change my answer to a wiki community please feel free to complete it I really don't know how to add variable for the crontab. – Kiwy Mar 01 '14 at 01:21
  • Much like crontab the env must be spelled out on OS X / macOS with launchd agents and daemons. It's easy to do, but important to remember. – uchuugaka Jun 29 '16 at 02:54
  • I would also add that OS X / macOS may overwrite your /etc/profile during any system update. Your user account doesn't actually own that. – uchuugaka Jun 29 '16 at 02:55
  • I wrote a small script that echo a $VARIABLE that is saved in ~/.profile, and it works, I mean, if I run ./foo,sh, it will print the variable value, why? – IAmJulianAcosta Oct 06 '16 at 04:16
  • One more piece of information from https://wiki.debian.org/EnvironmentVariables: You can "put all global environment variables, i.e. ones affecting all users, into /etc/environment but this file is read by PAM, not by a shell. You cannot use shell expansions here. E.g. MAIL=$HOME/Maildir/ will not work! There is no shell-agnostic and login-independent solution to the problem of how to configure the environment for all users, beyond the trivial cases that PAM can handle". – Jack Dec 02 '16 at 05:32
  • .... /etc/profile.d folder ..... Be carefull though this should not be use as a standard way of adding variable to env on Debian.

    @Kiwy Why do you say like that? What should be taken care of in Debian distros if one were to put a script there?

    – promaxdev Jan 26 '21 at 08:29
  • I am trying to inject an environment variable into a podman container and would like it to work if either bash or just sh is available. I can get it working with bash by editing /etc/bashrc but I cannot figure out what file to modify for just sh. Can someone help me? – refriedjello Jun 03 '23 at 20:38
100

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 "$@"
Graeme
  • 34,027
  • so, if I've added value pairs to my environment file, what causes those to load up? For example, after exiting vi editor (changes written successfully) , I'm expecting to write echo $MY_NEW_VARIABLE and see it print out - EDIT: I see they're there if I launch a new command shell – bkwdesign Jan 22 '16 at 00:28
  • Unsure if this is a personal issue or what, but I had to reboot my OS after setting this in /etc/environment, even if I restarted the bash terminal or typed source /etc/environment. So if you're having issues where the environment variable isn't perpetuating, try rebooting. – Blairg23 Jan 11 '18 at 01:04
  • @bkwdesign @Blairg23, /etc/environment is not a script, so it cannot be sourced. It's loaded at login time (and a handful of other system events) by PAM, so you can either logout/login or run a new login session in a terminal by $ su <your username here> to pick up the new values in that isolated session. The values will disappear once you exit, so it's probably less error-prone to just fully logout/login. – AaronDanielson Feb 12 '18 at 18:33
  • ubuntu 20.04 here, /etc/environment didn't work but creating a .sh file in /etc/profile.d/ did the trick, thanks :) – Elouan Keryell-Even Jul 30 '21 at 11:58
  • I tested the approach with just the /etc/environment file on CentOS 8 and I didn't need to restart I just needed to exit (log out) the terminal or create a new one (log in). – Eduardo Lucio Aug 04 '21 at 01:58
  • 1
    @Eduardo, careful with this. If your distro's login scripts are reading /etc/environment, that's great. But remember it only affects programs started under that login. If you want the environment change to affect other users, they will need to log in again too (and if you don't consider Kiwy's answer instead). Also, if you want the changes to affect system services, this may require a reboot. – Graeme Aug 09 '21 at 18:13
0

You don't specify which flavour of UNIX you are using. On FreeBSD (and Net and Open BSD) you can use /etc/login.conf. Full details in the man page but you can add something like:

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

to the default class and it will be set for all users regardless of shell or lack thereof.

There are also PAM modules that can do something similar if you are using PAM. e.g pam_env which reads from /etc/environment.

-1

I find "How to save these variables permanently ?" ambiguous.

All answers seem to suppose that it means permanence in the meaning of "after a reboot".
I stumbled here looking for a way to persist ENVs for a bash session so that a production app could be run with ENVs set in the same process.

I'll leave this answer here for the like-minded.
The solution is to put them in a script like so:

set-envs.sh:

#!/bin/sh
export ENV1="some value"
export ENV1="some value"

chmod +x set-envs.sh once to make it executable
and run it before the app with source (or it's shorthand . )

. ./set-envs.sh
./run-the-app.sh  # or such
  • 1
    There is no need to make a script executable if you source it, only if you execute it directly. It sounds like you actually want ENV1=Foo ENV2=bar ./run-the-app. – terdon Feb 06 '23 at 20:57