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=VALUEFor 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_HOMEUnsetting an environment variable
The following command will be used to unset the environment variable.
unset VARIABLENAMESet 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_homeExit 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…
Related Guides and Helpful Resources
If you found this guide helpful, check out these related step-by-step tutorials and category resources on ION HowTo:
- How to Replace AC Remote Batteries with a Ballpoint Pen: Lever out stubborn AAA batteries from deep remote slots without tools.
- Clean USB-C Port with Paper: Simple household trick to clean lint and debris from loose charging ports safely.
- IKEA KOPLA B22 to E27 Socket Converter Guide: Convert older UK bayonet lamp fittings to fit modern screw-in LED bulbs.
- DIY Hacks and Life Solutions: Explore our full collection of tutorials and step-by-step articles in this category.







Leave a Reply