🌍 Command: env (Environment Variables)
The env command is used to either display your current environment variables or to run a program in a modified environment. Environment variables are dynamic values that affect the way running processes behave on your Linux system.
1. The "Why"
For a developer and system administrator, env is the control center for configuration:
- Path Management: See your
$PATHto understand why a Java command or a LaTeX binary isn't being found by the terminal. - Debugging: Check which compiler flags, library paths, or API keys (like your Supabase credentials) are currently active.
- Desktop Environment Config: Identify your current Wayland or X11 settings for Hyprland and GNOME.
- Software Portability: Ensure a script runs with the correct interpreter (e.g., using
#!/usr/bin/env python3instead of a hardcoded path).
2. Basic Usage: Displaying Variables
Running env without any arguments will print a long list of KEY=VALUE pairs.
env
Common variables you’ll see:
USER: Your current username (e.g.,ahmed).SHELL: The path to your current shell (e.g.,/bin/zshor/bin/bash).PWD: Your current working directory.PATH: A colon-separated list of directories where the system looks for executable files.EDITOR: Your default text editor (e.g.,nvimorcode).
3. Running a Program in a Clean Environment
Sometimes you want to run a program without any of your personal configurations interfering (e.g., testing if a Java app works with default settings).
| Flag | Purpose | Command Example |
|---|---|---|
-i |
Ignore | env -i [command] (Starts with an empty environment). |
NAME=VAL |
Set Variable | env VAR=1 [command] (Runs the command with VAR set to 1). |
Example: Running a script with a specific temporary path:
env PATH=/tmp/test_bin ./my_script.sh
4. Practical Examples for Your Workflow
A. Finding a Specific Variable
If you want to check your Java home directory or your Supabase URL:
env | grep JAVA_HOME
B. The Shebang Line (#!)
When writing scripts for your tutorials, always use env in the first line. This makes the script work on any Linux distro, regardless of where the binary is installed:
- Better:
#!/usr/bin/env bash - Avoid:
#!/bin/bash
C. Checking Desktop Environment
If you're troubleshooting your Hyprland setup:
env | grep -E "XDG_CURRENT_DESKTOP|WAYLAND_DISPLAY"
5. env vs. export vs. set
| Command | Scope | Purpose |
|---|---|---|
env |
Current process | View exported variables or run a command in a new environment. |
export |
Current shell + children | Mark a variable to be passed to child processes. |
set |
Current shell only | Lists all variables (including local ones and shell functions). |
6. Pro-Tips
- Temporary Secrets: If you need to run a command with an API key but don't want it saved in your shell history, you can use
env:env API_KEY=12345 ./deploy_script.sh - Arch Linux Context: If you use different versions of compilers for your Master's research,
envallows you to switch environments per-terminal session without changing your global system settings. - Tutorial Tip: When teaching "Ahmed Codes" followers about security, emphasize that
envshows variables in plain text—so be careful when sharing screenshots of your terminal!
7. Summary Reference
| Goal | Command |
|---|---|
| List all variables | env |
| Filter variables | `env |
| Run with new var | env KEY=VAL [cmd] |
| Run with clean environment | env -i [cmd] |