This knowledge base will guide you in-depth about the Linux environment variables so that you can easily read and modify basic commands on your VPS hosting. Let’s get started!
What is Environment Variable?
Environment variables are dynamic values that directly affect the programs or processes running on a server. They are of different types that reside on every operating system. You can easily create, save, edit and delete these variables.
Linux environment variables are placeholders for all the information that is stored within the system that posses data of programs launched in shells or sub-shells.
A variable is an object that has been named and it contains data that is used by one or more application. That means it is a variable that contains a name and a value.
Variables – Variable is nothing but the data stored somewhere in the memory which is referenced by some indicator (variable name).
The value of Environment Variable can be the location of all executable files in the file system, the default editor that should be used, or it can be the system locale settings.
Thus, environment variables provide an easy way to share configuration settings between multiple applications and process in Linux.
Working of Environmental Variables :
Whenever you execute a shell session, in the backend, a process takes place to collect and compile information that should be available to the shell process and its child process. It is responsible to obtain the data from these settings from a variety of different files and settings on the system.
The environment is implemented in the form of a string that represents the key-value pairs. Multiple values are separated by semicolon (:) characters. The pair looks something like this :
KEY=value1:value2: .....
If the value contains white-space then the quotations are used :
KEY="value with spaced"
The KEY in such examples are variables. They can be of type (a) Environmental variables or (b) Shell variables
Commands for Linux Environment Variable
Some common Linux environment variable commands that can help you to understand the Linux working will be discussed in this tutorial. Make a note that in order to modify any variables, you will need to access your VPS by using SSH.
How to View Linux Environment Variables?
Printenv allows you to view the entire list of environment variables on your Linux distribution.
You will get a large output by simply using it on your Ubuntu system.
To manage the output you can use piping in a modifier :
printenv | less
Every line in the command contains the name of the Linux environment variable that is followed by = and the value.
e.g.:
HOME=/home/jack
where :
Home is a Linux environment variable. This variable contains the value set as /home/Jack directory.
Environment variables are not case sensitive but still, are mostly used in upper case.
In this example, the output of printenv displays all environment variables in uppercase.
But, the important point to note here is that Linux environment variables are case sensitive.
If you wish to see the value of a specific environment variable, you can do it by passing the name of that variable as an argument to the printenv command. The string will look like this :
printenv HOME
Output :
/home/jack
The other method to display the value of an environment is by using the echo command like this :
echo $USER
Output :
Jack
Creating a New Linux Environment Variable
Syntax :
export VAR="value"
where,
• export: command used to create a variable
• VAR: the name of the variable
• =: it shows that the following section is the value
• “value” : the actual value
Command :
export jack ="milesweb"
2) To change the value of Timezone variable
Command to view the current output :
date
To edit the current time, use the export command :
export TZ=" US/Pacific"
Related: How to Change Timezone in Ubuntu?
This way the variable value has been changed. To view the changes use date command, this gives you the output as per the changes made in the Linux environment variable.
3) To UNSET the Value of Linux Environment Variable
Syntax:
unset VAR
where,
• unset stands for the command itself
• VAR stands for the variable we want to unset
e.g: let’s unset the timezone variable using
unset TZ
This command will take the timezone to its default value which can be again checked by using date command.
The setting and unsetting of a Linux environmental variable via command line affects only the currently running sessions. In order to make your settings work continuously during the login sessions, you will have to defend the environment variables in your personal initialization file i.e. .bash_profile.
Local and Global Linux Environment Variable
Global variable :
In a computing program, Global variable is a construct and type of variable that is declared outside the function and thus, is accessible to all the functions throughout the program. A group of global variables are combined together to form the global environment that defines various aspects of a program or the environment when the program runs. A global variable is generally declared at the top of all functions, this is the reason that it can be used anywhere in the program.
Local variable :
A variable declared as local is given the local scope. That means it is visible only within the block of code in which it appears. In any function, a local variable has its meaning limited only within that function block.
Let’s take an example :
Global_var and local_var are the global and local variables respectively.
Var Global_val=60; Function Fun() { var local_var = 30; }
The global environment variables are visible from a shell session of any child processes that the shell spawns.
On the other hand, the local variable can be available in the shell only in which it is created.
For system environment variables, uppercase letters are used so as to differentiate them from normal environment variables.
Let’s see how you can set a Local Linux Environment Variable –
Note: in the below example, local_var is only visible in the current shell :
local_var=jack echo $local_var jack
To create a global environment variable use the export command:
export Global_var=Hello bash echo $Global_var
The output will be:
Hello
From all the above examples you might have understood how you can set Environment variables to your VPS. To explore further here is a list of some commonly used variables in Linux that you can try by yourself.
System Variable | Meaning | To view Variable Value Type |
---|---|---|
HOSTNAME | The name of your system (computer). | echo $HOSTNAME |
HISTFILE | It is a name of a file in which command history is saved. | echo $HISTFILE |
HISTFILESIZE | The maximum number of lines contained in the history file. | echo $HISTFILESIZE |
HISTSIZE | The total number of commands to remember in the command history. By default, its value is 500. | echo $HISTSIZE |
CDPATH | The search path for the cd command. | echo $CDPATH |
HOME | The home directory of the current user. | echo $HOME |
BASH_VERSION | It holds the version of this instance of bash. | echo $BASH_VERSION |
LANG | It is used to determine the locale category for any category not specifically select with a variable starting with LC_ . | echo $LANG |
IFS | The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is . | echo $IFS |
PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. | echo $PATH |
PSl | Your prompt settings. | echo $PS1 |
TERM | Your login terminal type. | echo $TERM export TERM=vt100 |
SHELL | Set path to login shell. | echo $SHELL |
DISPLAY | Set X display name. | echo $DISPLAY export DISPLAY=:0.1 |
EDITOR | Set name of default text editor. | export EDITOR=/usr/bin/vim |
TMOUT | The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. | echo $TMOUT |
Conclusion –
Operating basic Linux Environment variables is very easy, isn’t it?
Just be careful and do some research before performing any modification and do brush up your skills regularly. Did you find this tutorial helpful? Let us know in the comment section.