Linux environment variables: how to read and set them on a Linux VPS

In this tutorial, we will explain in depth what environment variables are, we will teach you how to make basic modifications and how to read commands in your . Let us begin!

What is a variable?

In computing, a variable is a location that allows a value to be stored that can be changed based on conditions or information from the program. Variables play an important role in programming, they allow developers to write flexible programs related to the operating system they work on!

What are Linux environment variables?

Environment variables are dynamic values ​​that affect programs or processes running on a server. They exist in all operating systems and their type may vary. Environment variables can be created, edited, saved, and deleted.

In Linux, environment variables are placeholders for information stored within the system that passes data to programs started in shells (command prompts) or sub-shells.

Commands for Linux environment variables

Let’s look at some common commands for using Linux environment variables that you might want to know about. Remember, before modifying any variable, you must access your using .

How to view environment variables in Linux

You can see the full list of environment variables for your version of Linux using the printenv command. Simple use of this in Ubuntu will provide a great output showing all the variables.

You can get more manageable output by adding details on the command line, such as:

printenv | less

Each line contains the name of the Linux environment variable followed by = and the value. For example:

HOME=/home/edward

This means that HOME is a Linux environment variable that has the value set to the directory /home/edward.

See also  How to use PHP to insert data into MySQL

Environment variables are usually uppercase, although you can also create lowercase environment variables. the output of printenv displays all environment variables in uppercase.

One important thing to note is that Linux environment variables are case sensitive. If you want to see the value of a specific environment variable, you can do so by passing the name of that variable as an argument to the printenv command. The entire string would look like this on the command line:

printenv HOME

Departure:

/home/edward

Another way to display the value of an environment variable is to use the command threw out in this way:

echo $USER

Departure:

Edward

How to create a new environment variable in Linux

The basic syntax of this command would look like this:

export VAR=”value”

Let’s break it down:

  • export: the command used to create the variable.
  • VAR: the name of the variable.
  • = indicates that the next section is the value.
  • “value”: the actual value

Let’s take a real example:

export edward = “”

Let’s see how you could change the value of the variable TZ (time zone):

First, check the time:

date

The command should display the current time.

After this, you can use the export command to alter the timezone:

export TZ=”US/Pacific”

Now that you’ve changed the value of the variable, you can check the time again using the command datewhich will generate a different time, according to the changes made in the Linux environment variable.

Revert the value of a Linux environment variable

For this you can use the command one set. The command syntax looks like this:

See also  8 of the best CMS to create a website in 2022

unset VAR

The parts of the command are:

  • one set: the command itself
  • VAR: the variable whose value we want to reverse

Simple truth? Now, as an example, let’s see how to reverse the timezone variable:

unset TZ

This will set the time to its default value, which you can check by using the date command once more.

Setting and reverting a Linux environment variable from the command line affects only your current sessions. If you want the configuration to persist across logins, you must define the environment variables in your personal initialization file, ie .bash_profile.

Local and global Linux environment variables

In computer programming terms, a global variable is one that can be used anywhere in the program. Whereas a local variable is defined in a function and can only be used in that function. For example, Global_var Y local_var are global and local variables, respectively:

VarGlobal_val=50; Function Fun() { var local_var =20; }

Linux environment variables can be global or local. Global environment variables are visible from within a shell session and in any child processes that the shell spawns. Instead, local variables can only be available in the shell in which they are created.

All system environment variables use uppercase letters to distinguish them from normal user environment variables.

How to set a Linux local environment variable

In the following example, local_var it is only visible in the current shell:

local_var=edward echo $local_var edward

We can create a global environment variable using the command export:

export Global_var=Hello bash echo $Global_var

Finally, the output will be:

Hello

conclusion

Congratulations, now you know all the basics of environment variables! Always remember to be careful, research what you’re modding, and keep advancing your skills! We hope that this tutorial has been helpful to you and that you have learned how to read commands in your !

See also  18 Best Online Collaboration Tools for Remote Work

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...