Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Lionel Shantarin




Posted Questions



Wait...

Posted Answers



Answer


Variables can be used repeatedly throughout the code or by your operating system to provide values. You can edit them, overwrite them, and delete them.

In this tutorial, I'll teach you what environment variables are and how to set them in Linux.

Environment variables are the variables specific to a certain environment. For example, each user in an operating system has its own environment. An admin user has a different environment than other users do, for example.

You might declare an environment variable only required by your user (for example a secret token) that doesn't need to be exposed to other users.

Here are some examples of environment variables in Linux:

These environment variables vary based on the current user session.

The command used to display all the environment variables defined for a current session is env.

Here is the output for my session:

There are two ways to print the already defined environment variables:

Let's print the value of the variable SHELL using both methods. Here's an example of printing using printenv:

And here's an example of using echo:

The basic syntax to define an environment variable is as follows:

Let's define an environment variable, list it, and print its value.

However, the variables defined using this method are stored for the current session only. They won't be available for the next session.

Let's verify by opening a new session and printing the variable's value.

But, we can make the definitions persistent as shown in the next section.

To make the JAVE_HOME variable persistent, edit the file .bashrc and define its value in it.

The .bashrc is a script file that's executed whenever a user logs in. It is hidden and located in the user's home directory by default.

I have edited my .bashrc file as follows:

For the changes to take effect, update the .bashrc  file using the source command:

Let's verify by opening a new session.

Sometimes you might need to define a global environment variable that is accessible by all users.

For that, we need to first declare a variable and make changes in relevant files where environment variables are read from.

Let's go step by step.

2.  Edit the following files:

For the changes to take effect, use the command source /etc/environment.

Time to test!

Now, I'll switch the user to the root user and verify if I can access the variable GLOBAL_VARIABLE.

It worked! I have been able to access the global variable defined by the user Zaira through the root user as well. The same would apply to other users too. So now you also know how to define global environment variables.

In this tutorial, you learned how to create and define environment variables in Linux. You also learned how to make them persistent so that you can use them across multiple sessions.

What’s your favorite thing you learned here? Let me know on Twitter!

You can read my other posts here.


Answer is posted for the following question.

Bash where to set environment variables?

Answer


Description Miami is a city in and county seat of Ottawa County, Oklahoma, United States, founded in 1891. Lead and zinc mining were established by 1918, causing the area's economy to boom. This area was part of Indian Territory. Wikipedia


Answer is posted for the following question.

What is in miami oklahoma?


Wait...