How to permanently set environmental variables

  • 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 ?

    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.

    Careful, there is more to this story than initially appears. I invite you to check my answer.

  • Kiwy

    Kiwy Correct answer

    7 years ago

    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
    

    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.

    This `.profile` in `/etc/` but I don't how to set the variables in this `.profile` please tell me

    `.profile` is in your home directory not `/etc/`

    How to check this `.profile` in my home directory??

    `[[email protected] etc]$ cat ~/.profile cat: /home/Admin/.profile: No such file or directory [[email protected] etc]$ `

    what is your shell ?

    I'm using bash shell

    @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

    sorry!! works fine .. Thank you foe efforts while answering my question and thank you for answering my question

    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 thanks, i proceed to an edit :-)

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

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

    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.

    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.

    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?

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

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM