Environment Variables in Linux

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • User AvatarANKUSH THAVALI
  • 08 Sep, 2023
  • 0 Comments
  • 2 Mins Read

Environment Variables in Linux

Environment Variables in Linux

In this blog ,  we will see how to set environment variables in Linux. A variable stores a value and gives you flexibility to call the variable in any shell script. Linux environment variables are backbone of Linux operating system. You can define a linux variable using export command.
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. 
Echo Command :  If you want to check the value of a stored environment variable then you can use Echo command.
e.g.

echo "Current website is $MY_WEBSITE"
echo "You are reading $ARTICLE"

Env command : To list all system variables and local variables simply use env command.
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!