Skip to content

Instantly share code, notes, and snippets.

@rw3iss
Last active April 17, 2026 10:25
Show Gist options
  • Select an option

  • Save rw3iss/a09c62256c565150cd2ae77535043246 to your computer and use it in GitHub Desktop.

Select an option

Save rw3iss/a09c62256c565150cd2ae77535043246 to your computer and use it in GitHub Desktop.
envm: shell environment variable manager for ~/profile/.env

envm — environment variable manager

A shell utility to list, read, set, and delete variables in your ~/profile/.env — the single source of truth for all shell environment variables.

Changes take effect immediately in the current shell — no need to restart or re-source manually.

Install

Add envm as a shell function so it runs in your current shell context (required for live export/unset to work):

# In ~/profile/.aliases_utils or ~/.zshrc / ~/.bashrc:

envm() {
    local ENV_FILE="${PROFILE_DIR:-$HOME/profile}/.env"
    # ... (see envm script file for full function body)
}

Or source the provided script directly:

# ~/.zshrc or ~/.bashrc
source ~/bin/envm-fn   # if saved as a function file

Note: If run as a plain script (~/bin/envm), export and unset only affect the subprocess — changes won't be live in your current shell. Define it as a function instead.

Set PROFILE_DIR to override the default location:

export PROFILE_DIR=/path/to/your/profile

Usage

envm                    — list all variables
envm KEY                — show value of KEY
envm KEY value          — set KEY=value (prompts to confirm if already exists)
envm -d KEY             — delete KEY (prompts to confirm)

Examples

# List all env vars
$ envm
/home/user/profile/.env

  GH_TOKEN                            ghp_xxxxxxxxxxxx
  OLLAMA_API_BASE                     http://127.0.0.1:11434
  JAVA_HOME                           /usr/share

# Read a single value
$ envm OLLAMA_API_BASE
OLLAMA_API_BASE=http://127.0.0.1:11434

# Add a new variable (live in current shell immediately)
$ envm MY_API_KEY abc123
Added: MY_API_KEY=abc123
$ echo $MY_API_KEY
abc123

# Update an existing variable
$ envm GH_TOKEN ghp_newtoken
Current: GH_TOKEN=ghp_oldtoken
Replace with 'ghp_newtoken'? [y/N] y
Updated: GH_TOKEN=ghp_newtoken

# Delete a variable
$ envm -d MY_API_KEY
Delete MY_API_KEY=abc123
Confirm? [y/N] y
Deleted: MY_API_KEY

How it works

  • Reads use grep on the .env file — no shell state required
  • Writes use sed -i for in-place editing
  • After any write, .env is sourced and the var is live immediately (when run as a shell function)
  • Deletes call unset in addition to removing the line from the file
  • Variables are stored as export KEY=value lines, compatible with any POSIX shell
#!/usr/bin/env bash
# envm — read and manage environment variables in ~/profile/.env
#
# Usage:
# envm list all vars
# envm KEY show value of KEY
# envm KEY value set KEY=value (prompts if already exists)
# envm -d KEY delete KEY
ENV_FILE="${PROFILE_DIR:-$HOME/profile}/.env"
R=$'\033[0m'
C_KEY=$'\033[1;37m'
C_VAL=$'\033[38;5;114m'
C_OLD=$'\033[38;5;208m'
C_DIM=$'\033[2m'
C_ERR=$'\033[31m'
if [[ ! -f "$ENV_FILE" ]]; then
echo "${C_ERR}Error: $ENV_FILE not found.${R}"
exit 1
fi
# --- Helpers ---
_get_val() {
grep -E "^export $1=" "$ENV_FILE" | sed "s/^export $1=//"
}
_set_val() {
local key=$1 val=$2
if grep -qE "^export $key=" "$ENV_FILE"; then
sed -i "s|^export ${key}=.*|export ${key}=${val}|" "$ENV_FILE"
else
echo "export ${key}=${val}" >> "$ENV_FILE"
fi
}
_delete_val() {
sed -i "/^export $1=/d" "$ENV_FILE"
}
# --- No args: list all ---
if [[ $# -eq 0 ]]; then
echo "${C_DIM}$ENV_FILE${R}"
echo ""
grep -E '^export [A-Z_a-z]' "$ENV_FILE" | while IFS= read -r line; do
key=$(echo "$line" | sed 's/^export //; s/=.*//')
val=$(echo "$line" | sed "s/^export ${key}=//")
printf " ${C_KEY}%-35s${R}${C_VAL}%s${R}\n" "$key" "$val"
done
echo ""
exit 0
fi
# --- Delete flag ---
if [[ "$1" == "-d" ]]; then
if [[ -z "$2" ]]; then echo "Usage: envm -d KEY"; exit 1; fi
key="$2"
existing=$(_get_val "$key")
if [[ -z "$existing" ]]; then
echo "${C_ERR}Not found: $key${R}"
exit 1
fi
echo "Delete ${C_KEY}$key${R}=${C_OLD}$existing${R}"
printf "Confirm? [y/N] "; read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
_delete_val "$key"
echo "Deleted: $key"
source "$ENV_FILE"
else
echo "Cancelled."
fi
exit 0
fi
# --- One arg: show value ---
if [[ $# -eq 1 ]]; then
key="$1"
val=$(_get_val "$key")
if [[ -n "$val" ]]; then
printf "${C_KEY}%s${R}=${C_VAL}%s${R}\n" "$key" "$val"
else
echo "${C_ERR}Not found: $key${R}"
exit 1
fi
exit 0
fi
# --- Two+ args: set value ---
key="$1"
val="$2"
existing=$(_get_val "$key")
if [[ -n "$existing" ]]; then
printf "Current: ${C_KEY}%s${R}=${C_OLD}%s${R}\n" "$key" "$existing"
printf "Replace with '%s'? [y/N] " "$val"; read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
_set_val "$key" "$val"
printf "Updated: ${C_KEY}%s${R}=${C_VAL}%s${R}\n" "$key" "$val"
source "$ENV_FILE"
else
echo "Cancelled."
fi
else
_set_val "$key" "$val"
printf "Added: ${C_KEY}%s${R}=${C_VAL}%s${R}\n" "$key" "$val"
source "$ENV_FILE"
fi

envm — environment variable manager

A shell script to list, read, set, and delete variables in your ~/profile/.env file — the single source of truth for all your shell environment variables.

Install

curl -o ~/bin/envm https://gist.githubusercontent.com/rw3iss/raw/envm
chmod +x ~/bin/envm

Make sure ~/bin is in your PATH, then optionally add an alias:

# in ~/.zshrc or ~/profile/.profile
alias env=envm      # shadows system env — use only in interactive shells

The script reads from ~/profile/.env by default. Override with:

export PROFILE_DIR=/path/to/your/profile

Usage

envm                    — list all variables
envm KEY                — show value of KEY
envm KEY value          — set KEY=value (prompts to confirm if already exists)
envm -d KEY             — delete KEY (prompts to confirm)

Examples

# List all env vars
envm
#   GH_TOKEN                           ghp_xxxxxxxxxxxx
#   OLLAMA_API_BASE                    http://127.0.0.1:11434
#   JAVA_HOME                          /usr/share

# Read a single value
envm OLLAMA_API_BASE
#   OLLAMA_API_BASE=http://127.0.0.1:11434

# Add a new variable
envm MY_API_KEY abc123
#   Added: MY_API_KEY=abc123

# Update an existing variable (shows current value, prompts)
envm GH_TOKEN ghp_newtoken
#   Current: GH_TOKEN=ghp_oldtoken
#   Replace with 'ghp_newtoken'? [y/N] y
#   Updated: GH_TOKEN=ghp_newtoken

# Delete a variable
envm -d MY_API_KEY
#   Delete MY_API_KEY=abc123
#   Confirm? [y/N] y
#   Deleted: MY_API_KEY

How it works

  • All reads use grep on the .env file directly — no shell state required
  • Sets and deletes use sed -i for in-place editing
  • After any write, the .env is sourced to apply changes to the current shell session
  • Variables are stored as export KEY=value lines, compatible with source in any POSIX shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment