← Back to blog
Linux Environment Variables: A Simple Guide

Linux Environment Variables: A Simple Guide

authors photo
Written by Ola
Friday, October 28th 2022

96.3% of the top one million web servers run on Linux, and Linux environment variables are one of those basic things all software programs rely on to run.

Onboardbase provides a secret management tool that helps individual developers and teams manage environment configurations securely, including Linux environment variables. Even though Linux env vars are commonplace, we know from first-hand experience it isn’t uncommon for security breaches to occur because of bad management practices: 37% of all surveyed engineering teams have experienced a security breach, and 47% of these security breaches are caused by negligence!

This article will teach you everything you need to know about creating and managing Linux environment variables securely and efficiently in 5 minutes, so let’s get started now.

What Are Linux Environment Variables

Linux environment variables are constants available to any programs on the Linux operating system. They are commonly used to store essential configuration parameters as strings of characters to orient how a program behaves. You can use them for anything from storing database passwords to locating home directories.

Environment variables follow the format <NAME>=<VALUE>:

DATABASE_PASSWORD="|j-m@|m1[DIP8$Dms766El$-)99"Jz#O9ySPl!/:PZn5DrHBv9[:l<2'<SpD=Nn"
DEV_LOCATION="/home/onboardbase/dev"
WBESITE_DOMAIN="https://onboardbase.com"

A variable’s scope refers to the parts of a program or environment that can access a given variable. An environment variable can be scoped globally or locally:

  • Global environment variables are accessible from anywhere on the machine.
  • Local environment variables can only be accessed from the specified sub-directory by programs or scripts running on it.

Common Linux Environment Variables

UNIX operating systems provide handy environment variables out of the box, including:

  • USER – The currently logged-in user.
  • HOME – The home directory of the user.
  • LOGNAME – The name of the user.
  • PWD – Current working directory.
  • UID – User’s unique identifier.

Why You Need To Use Linux Environment Variables

  • To ease development - Linux environment variables make development easier by allowing users to store useful information that will be available from any program.
  • To avoid hard-coding constants - You can avoid repeating yourself by using a Linux environment variable instead of hard-coding constants directly into your program. It not only represents a considerable time gain because you don’t have to copy/paste the same value every time it’s used or changed but also a security best practice that prevents leaking sensitive data: the environment variable is scoped to the current environment, and doesn’t end up in code repositories on Github.

How to use Linux environment variables

You can perform various operations with Linux environment variables using their specific commands. As you dive deeper into this section, you will learn more about how you can perform these operations with code samples and descriptions. We will use commands like env, printenv, set, unset, export, etc., to interact with our environment variables.

  • env: You can use the env command to get information about variables, such as their values and the environment they were declared in.
  • set: The set command in Linux is used for setting the value of a variable. it is used to overwrite the data in files or variables.
  • unset: The unset command in Linux is used to delete the value from a variable or clear it. It is used primarily when you want to remove a previous value.
  • printenv: The printenv command is used to print environment variables, run commands, and displays details about the current shell session.
  • export: The export command is used to sets the environment variables. But the variable is temporary, if the session logs out or closes, the scope of the variable will vanish.

1. Create Linux Environment Variable

Using the export command, we can create and set values for our Linux environment variables. As mentioned earlier, the export command is used to sets the environment variables. These variables created are temporary and specific to your current session.

export ONBOARDBASE_HOME=/opt/openjdk11

To create a permanent environment variable, you must modify the .bashrc file in your home directory and add a line that exports the environment variable you want to make permanent.

Run the command below.

nano .bashrc

Result:

Result

Edit the file and add export ONBOARDBASE_HOME=/opt/openjdk11. This will store as a permanent environment variable.

To save changes, run the command below:

source .bashrc

2. Read Linux Environment Variable

We can get a list of all of our Linux environmental variables by using the printenv or env command. As mentioned earlier, the printenv or env command can be used to print environment variables.

env

Result:

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35;
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=en_US
S_COLORS=auto
XDG_SESSION_ID=5
USER=linuxize
PWD=/home/linuxize
HOME=/home/linuxize
SSH_CLIENT=192.168.121.1 34422 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/linuxize
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LANGUAGE=en_US:
LOGNAME=linuxize
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv
ONBOARDBASE_HOME=/opt/openjdk11

Get a specific variable:

printenv ONBOARDBASE_HOME

Result:

/opt/openjdk11

3. Update Linux Environment Variable

We can update the values of Linux environment variables using the export command. While the export command can be used to create environment variables, it can also be used to update the value of an environment variable.

export ONBOARDBASE_HOME=Update/opt/openjdk11
printenv ONBOARDBASE_HOME

Result:

Update/opt/openjdk11

4. Delete Linux Environment Variable

We can delete Linux environment variables using the unset command. As mentioned earlier, the unset command in Linux is used to delete the value from a variable or clear it.

unset ONBOARDBASE_HOME

This article has covered a lot about Linux environment variables. You should now be able to perform some basic CRUD(Create, Read, Update and Delete) operations with the available commands that Linux provides.

5. Use Onboardbase To Manage Linux Environment Variables

What if you could have all the flexibility of a Linux environment variable without compromising security? With an app secret manager like Onboardbase, you can eliminate secrets management pain points like using similar local environment variables across different Git repositories or rotating secrets to avoid security breaches.

Onboardbase stores your Linux environment variables in a single repository. For example, we define two environment variables to connect to a database in the following screenshot:

Result

You can then use Onboardbase CLI to pull your Linux environment variables and their values in a few seconds:

Result

Onboardbase helps you share your Linux environment variable securely. For example, you can grant access to any environment variable you want to share. See the screenshot below:

Result

You can then inject any Linux environment variables easily in a single command line:

onboardbase build --command="your project's build command"

Use Onboardbase To Secure Linux Environments

Onboardbase makes developers’ lives easier by making sharing and integrating environment variables simple and delightful. A dashboard centralizes app configurations for the whole team, and a command-line interface is available to integrate env variables in any workflow. The best part? You can get started for free in a minute.

Subscribe to our newsletter

The latest news, articles, features and resources of Onboardbase, sent to your inbox weekly