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:
- Login/Non-login shell
- Interactive/Non-interactive shell
bash
- Bash as login shell will load
/etc/profile
, ~/.bash_profile
, ~/.bash_login
, ~/.profile
in the order
- Bash as non-login interactive shell will load
~/.bashrc
- 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.
Given the file you show, you should be able to do:
(set -f ; IFS='
' ; env - $(cat /path/to/file) /path/to/your/program
)
If it doesn't work then it is only because you need to format your environment file first. Here's an example:
(set -f ; IFS='
' ; env - $(cat) printenv
) <<\ENV
variable1=value1
variable2=value2
variable3=value3 an$d s'om\e m"ore
ENV
###OUTPUT###
variable1=value1
variable2=value2
variable3=value3 an$d s'om\e m"ore
I at first thought you could do it through the shell - but it will probably set some of its own environment before calling your program. But I can at least demonstrate that the arguments are assigned correctly:
(set -f; IFS='
' ; env - $(cat) sh -c 'echo "$variable3"'
) <<\ENV
variable1=value1
variable2=value2
variable3=value3 an$d s'om\e m"ore
ENV
###OUTPUT###
value3 an$d s'om\e m"ore
Still, if you would prefer to source it, here's how you can using the shell:
(echo '$1'; cat; echo '$2') <<\ENV |\
env - sh -s -- 'set -a' printenv
variable1=value1
variable2=value2
variable3='value3 an$d s'\''om\e m"ore'
ENV
###OUTPUT###
PWD=/home/mikeserv/test
SHLVL=1
variable1=value1
variable2=value2
variable3=value3 an$d s'om\e m"ore
_=/usr/bin/printenv
Notice that I removed the $IFS
stuff - that's not necessary this way - but I did have to get specific about the quotes in the file. Here I'm essentially .dot
sourcing stdin
- reading the |pipe
as input - but you can use any file. I use set -a
before reading the input file to set the --allexport
option.
That is a result of using bash
's sh
- it adds $PWD
, $SHLVL
and $_
. With dash
it is a little better. And dash
doesn't add a bunch of exports either, so you can specify the -a
parameter on the command line:
(cat; echo '$1') <<\ENV |\
env - dash -sa -- printenv
variable1=value1
variable2=value2
variable3='value3 an$d s'\''om\e m"ore'
ENV
variable1=value1
variable2=value2
variable3=value3 an$d s'om\e m"ore
PWD=/home/mikeserv/test
Only $PWD
comes through.
Best Answer
Non-existent variables will always evaluate to an empty string when expanded as
$FOO
or (equivalently)${FOO}
, and there's no harm in depending on that, except in one particular case:If someone has called
set -u
in the current shell before you try to use that variable, they've enabled this setting:This means that if you're writing a function that's designed to be sourced into a script that someone else is in control of, you may need to be paranoid about using unset variables - otherwise, if they used
set -u
prior to calling your function, their script would exit with an error message the first time you tried to expand an unset variable.If you're writing your own script, there's no harm in counting on unset variables expanding to the empty string.
EDIT - Also, just a thought - since you're making the whole thing conditional on whether the terminfo color capabilities are available for your terminal, why not actually use terminfo to generate the sequences, rather than hardcoding the vt100 values? Something like:
This may gain you some portability across other terminals (though, admittedly, the number of terminals that don't use the codes you showed is small and shrinking). It may also lose some portability, as some capabilities may not exist on some platforms depending on how correct the terminfo definitions are. YMMV.