- Kiran Dalvi
- 08 Sep, 2023
- 0 Comments
- 2 Mins Read
Environment Variables in Linux
Environment Variables in Linux
syntax : export = e.g. export MY_WEBSITE=learnomate.org export ARTICLE="Environment Variables in Linux" export dba ='Learnomate is "BEST" Training institute.'
Please Note
- Always use UPPERCASE for variable names
- Put value under ” “(double quote) if it contains spaces or single quotes.
- Put value under ‘ ‘ if it contains double quotes.
- You have to use $ when you want to call a variable’s stored value.Â
e.g. echo "Current website is $MY_WEBSITE" echo "You are reading $ARTICLE"
env To filter all the variables listed by env command env | grep MY --> prints all vars containing MY env | grep HOME --> prints all vars containing HOME
Scope of Environment Variables :
When you set environment variables using linux export command, these variables scope is limited to only session level. once You close the terminal and login again, the variables are cleared from the system.
To make the variables permanent so that they are available when you login again, you must set the variables inside Bash profile.
vi .bash_profile           –> user’s home location    Â
 export MY_WEBSITE=learnomate.org
export ARTICLE=”Environment Variables in Linux”
Â
You need to source (or load) the .bash profile for Linux to load new environment variables
source .bash_profile
echo $MY_WEBSITE
echo $ARTICLE
The variables set under .bash profile are only available to the particular user.
For example, if Oracle user sets environment variables under its own .bash profile, then its accessible only to Oracle user.
To define system wide environment variables, add variables to /etc/environment file.
As root user:
vi /etc/environment
 export SYS_VAR=”System Wide Variable”
Logout and login. Now SYS_VAR is available to all the users on Linux server!