Linux how o set environment variable

Linux how o set environment variable

Linux how o set environment variable

How to set environment variable in Linux.

Set environment variables in Linux

To set a new environment variable, use the export command.

export NAME=VALUE

For example to set the environment variable called TEST_HOME you can run this command.

export TEST_HOME=/home/rex.red/test_service_home

This prints the value of the variable you set earlier.

echo $TEST_HOME

Unsetting an environment variable

The following command will be used to unset the environment variable.

unset VARIABLENAME

Set a persistent environment variable

If you set the variable from the shell, once you log out of your session, the variable is lost. To make an environment variable persistent across sessions you can export the variable in the user’s profile script.

vim ~/.bash_profile
# User specific environment and startup programs
export TEST_HOME=/home/rex.red/test_service_home

Exit vim :wq Logout and reconnect

[dragos@localhost ~]$ echo $TEST_HOME
/home/rex.red/test_service_home
[dragos@localhost ~]$

Now the variable will be set when your session starts.

View current environment variables

To view the current environment variables use the -p option.

[dragos@localhost ~]$ export -p
[dragos@localhost ~]$ echo $TEST_HOME
/home/test_service_home
[dragos@localhost ~]$ export -p
declare -x DISPLAY="localhost:10.0"
declare -x GDK_BACKEND="x11"
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/home/dragos"
declare -x HOSTNAME="localhost.localdomain"
declare -x LANG="en_GB.UTF-8"
declare -x LOGNAME="dragos"
declare -x OLDPWD
declare -x PATH="/home/dragos/.local/bin:/home/dragos/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin"
declare -x PWD="/home/dragos"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x TEST_HOME="/home/test_service_home"
declare -x USER="dragos"
declare -x XDG_DATA_DIRS="/home/dragos/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share"
declare -x XDG_RUNTIME_DIR="/run/user/1004"
declare -x XDG_SESSION_ID="77"
[dragos@localhost ~]$

This works on any Linux distribution, Redhat based, Debian based, etc…



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.