Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
2  
The other answers on this page are great. One small recommendation would be to add /usr/lib/oracle/11.2/client64/lib in a new file under the /etc/ld.so.conf.d/ path. Then you don't need to set LD_LIBRARY_PATH, see also here. – Elliott Frisch Feb 28 '14 at 18:19
up vote 74 down vote accepted

You can add it to the file .profile or .bashrc or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded.

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 ~/.bashrc
#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 ~/.zshrc
#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

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 /etc/profile or /etc/environment.
In this case follow the syntax you see already present in your file.

share|improve this answer
    
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
3  
.profile is in your home directory not /etc/ – Kiwy Feb 28 '14 at 13:45
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
3  
@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
1  
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 1 '14 at 1:04

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 "$@"
share|improve this answer
    
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 at 0:28

when you install oracle, oracle asked that you run some scripts before clicking ok. That script put a dummy setting in the .bash_profile in oracle user home directory. To see the file, ls -al will show all hidden files.

type nano "bash_profile" without the side quote to open the file. Make changes to the file to reflect your hostname, and appropriate sid name. check any other settings that need modification. press control x to save and type y when asked if you want to save. Press the return key to save. Restart the computer. Logging as oracle user. start the database by typing

sqlplus / as sysdba

startup  
share|improve this answer
    
How does this differ from the accepted answer? – roaima Sep 3 '15 at 13:10

protected by Community Oct 29 '15 at 14:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.