Define and use environment variables in Linux & Python

Created: Sun 09 Apr 2023 Updated: 9 months ago

article-featured-image

This article is about Environment variablesand their uses in Linux and Python as well. Most of the time, Environment variables are perfect when we need to store sensitive data and then access that data using a script. There are in-build environment variables like PWD and USER, where PWD gives us the Present working directory and USER gives us the username of the currently logged-in user.

System defined environment variables

There are many system defined variables that always come in handy. In the terminal, use this command to list all the variables:

$
env

You'll see the list of all system-defined environment variables along with their values in the terminal. However, when we are working on a specific project, there is always the need to define custom variables and utilize them throughout the project.

Defining variables using export command

There are a couple of ways to define your own variables and every way has its pros and cons. The fastest and quickest method is to use export command in the terminal:

$
export USERNAME="protocolten"

This command will set new variable by the name of USERNAME and the value will be protocolten. This is by far the fastest way to define a variable but the downside is that It's temporary.

The lifespan of the variable defined using this method is the current terminal session. As soon as you close the terminal, variable will be erased. While defining variables using export command, we have to use \ (back slash) to inserting special character. Suppose we need to define a variable that contains the password, then the correct way to define variable with special character is:

$
export PASSWORD="Password\@123"

Run the below command to print the value of the environment variable in terminal:

$
echo $PASSWORD
variabl-export-check This way we can add special characters in a variable while using export command.

Defining variables using .env file

To make a persistent variable that withheld the terminal close or even the system restart, we need to define variables using another method that is most convenient and reliable. First, create and open a new file by.env name in the system using nano ~/.env. In the file, define environment variables as:

USERNAME=protocolten
PASSWORD=password@123

Now save the file and run source ~/.env command to apply the changes and update the environment variables list for the current shell. To check if the newly created variables are working or not, use this command:

$
echo $USERNAME $PASSWORD
variable-check-from-env-file

This way you can define and use custom environment variables in Linux. Now the variables defined using this method will also last the current terminal session. After closing and re-opening the terminal, you have to load the variables by using source .env command.

Why defining environment variables in '.env' file is better than using 'export' command even when variables defined using both methods only last current shell?

If there are only one or two variables and we don't need to use them repeatedly, then defining them using 'export' method is usually considered a better option. However, if there are multiple variables that we need to use repeatedly, then '.env' method is always considered as better option as all the variables can be loaded together by using 'source .env' command, instead of exporting them one by one using 'export' method.

Defining variables using .bashrc file

If you want to create permanent variables or want them to persist across system reboots or you don't want to load them each time after re-opening the terminal, then defining environment variables in .bashrc file is the most appropriate method.

You can either create variables for a specific user by defining them in the user's .bashrc file or if your goal is to create variables that should be available globally for all users, then you should define them in system's .bashrc file.

  • Define variables for specific user: nano ~/.bashrc
  • Define variables globally for all users: sudo nano /etc/bash.bashrc

After defining variables in .bashrc file, either reboot your system or run source ~/.bashrc or source /etc/bash.bashrc command to load newly defined variables. By using this method, you can simply use these variables without loading them for the current shell.

Using environment variables in Python

You can use both system-defined environment variables and user-defined environment variables in your Python script. There are many packages that you can use to achieve this. The package that I'll be using is python-decouple for accessing variables in Python code. To install the package:

(venv) $
pip install python-decouple

Make sure you install the package in correct virtual environment and that you are using the same environment to run your Python code. Now open a new python script file using and paste the below code:

from decouple import config

usr = config('USERNAME')
pas = config('PASSWORD')
dir = config('PWD')
print(usr, pas, dir)
Now execute this python file. You will get similar output as shown in below image: variabl-check-python

In this Python script, we have accessed two type of variables. USERNAME and PASSWORD are user defined variables that we define previously in .env file. PWD is system defined variable that is used to get path of the present working directory. Just make sure that the .env file and python script file both are located in the same directory.

We have discussed the environment variables and their defining process using various methods. However, defining variables in .env file is a better choice in my personal opinion. We can load the file whenever or wherever we need to use the sensitive data in the script.

System and User-defined environment variables
protocolten-admin

Author: Harpreet Singh
Server Administrator

Suggested Posts:
LINUX post image
Install LAMP on Ubuntu or CentOS with firewall configuration

This tutorial will be focused on installing the LAMP stack (Linux Apache MySQL PHP) …

LINUX post image
Configure Firewalld in Linux

Firewalld is a powerful dynamic firewall management tool whose sole purpose is to manage firewall …

CLOUD post image
Python Lambda function to convert S3 bucket images into PDFs

AWS Lambda is amazing. It's a serverless platform where you don't have to manually …

LINUX post image
Block level full disk cloning using dd in Linux

Taking a backup of a system is very sensitive and critical process and sometimes …

LINUX post image
Create Custom Filter to replace characters in Django

Django template filters are amazing. Formatting data in a variety of ways is so …

Sign up or Login to post comment.

Sign up Login

Comments (0)