Skip to content

Environment Variables In Gnu/Linux

Published: at 03:30 AM
|
environment-variable

As the name suggests, an environment variable shapes the environment you are working in. Once you set an environment variable and run a command or application, it can read the variable and act differently.

Why are they used?

Examples:

export GOPATH=$HOME/go

Variable should be defined in the same command:

API_KEY="API_SECRET_KEY" node main.js

What does export do?

  1. KEY=VALUE
  1. export KEY=VALUE

What is $PATH?

Consider I have built a go application that takes two numbers and multiply it. I name the output binary multiply. I can run it like:

/home/seraj/multiplier/multiply 4 6
# 24

If I need to run this app I have to address its path whenever I want to run it. Alteratively I can set an $PATH environment variable in my shell that tells my shell where to look for binaries. Once set, I can run the previous command like this. PATH environment variable is a colon-separated list of directories the shell scans to locate executable files:

multiply 3 7
# 28

Other examples are when one needs to

pnpm global add http-server
go install github.com/air-verse/air@latest

To add go binaries to my PATH:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

More Inspection on the PATH Variable

It’s a given when you can run binaries like ls, cp, grep and etc. so the path to these binaries is already included in your shell’s PATH. To confirm this fact:

which grep
# /usr/bin/grep

echo $PATH
# /home/seraj/.local/share/pnpm:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

As you can see in the results this path /usr/bin is also included.

How to set environment variables permanently?

Setting envs in different shells are different.

Bash:

To add env: add your env to ~/.bashrc

TO update PATH: update it in ~/.profile

Fish

To Add env:

set -x GOPATH $HOME/go

To update PATH:

fish_add_path $GOPATH/bin

Read more details

Examples

Some simple real world examples:

GoLang

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Printf("We are running our app in %s mode.", os.Getenv("RUN_MODE"))
}

Run the app:

RUN_MODE=PRODUCTION go run main.go
# We are running our app in PRODUCTION mode.

Docker

docker run -e RUN_MODE=PRODUCTION app-image:tag

Docker Compose

services:
  image: app-image:tag
  environment:
    - RUN_MODE=PRODUCTION

Docker compose itself reads from .env as default for its own environment variables:

services:
  image: app-image:tag
  environment:
    - RUN_MODE=PRODUCTION
    - PASSWORD=${PASSWORD}

if you store a file beside your docker-compose file with name .env it will automatically reads it into the docker compose.

Example .env:

PASSWORD="MY_SECRET_PASSWORD"