Batch System Environment Variables - chrisbitm/python GitHub Wiki

Batch System Environment Variables are values that define aspects of the Runtime Environment in a Batch Processing System. They allow for communication between the OS and the Batch Scripts (Or Programs). Think of them as Placeholders or Settings that the System or a Script can reference to make decisions or adaptations to its behavior.

Scope

Changes made to environment variables are permanent, but variables set in a terminal session (e.g., via set in Windows or export in Linux) exist only for that session. Once the terminal is closed, those changes are lost unless explicitly made persistent (e.g., by editing .bashrc or system settings).

Local

Temporary variables exist only within the session in which they are defined (e.g., MY_VAR="Hello" in Linux or set MY_VAR=Hello in Windows). They are removed once the session ends.

Global

Persistent variables remain accessible across all sessions and system reboots, such as those added to configuration files (.bashrc for Linux/macOS or Environment Variables settings in Windows).

Types of Environment Variables

  • User-defined Variables are created within Scripts to store Temporary Data or Configuration Settings.
  • System Variables are predefined by the Operating System, such as COMSPEC, PATH, or TEMP.

Common System Variables

  • COMPSEC IS the direct path that points to 'CMD.exe'
  • PATH allows Executable (exe) Files to be run from the Terminal.
  • TEMP stores Temporary Files such as Cookies from Webpages along with other Data. Though, only a Temporary Folder, is necessary for various programs to store this Data.
  • USERPROFILE is set to C:\Users\<Username> where <Username> is the Account's name at login. All the Home Folders are stored here. In Unix-based Systems, the is called Home

Pitfall and Remedy

It’s generally not a good idea to delete ANY System Environment Variables. Deleting or modifying these variables if you do not know what you are doing can disrupt how programs operate, potentially leading to errors, crashes, or an inability to run certain commands from the Shell.

To prevent accidental deletion, it is generally a good idea to make a Backup. There are various ways to make a backup and one such method can be done by creating a simple Batch Script.

Example of Saving a Backup

@echo off
setlocal enabledelayedexpansion

:: Check if C:\Backup directory exists, and create it if it doesn't
if not exist "C:\Backup" (
    mkdir "C:\Backup"
    echo Created C:\Backup directory.
)

:: Backup environment variables to a file
(for /F "tokens=1,* delims==" %%A in ('set') do (
    echo %%A=%%B
)) > "C:\Backup\env_backup.txt"

ECHO Backup complete! Check C:\Backup for env_backup.txt.
  • Open a Text Editor and copy and paste this code and in Notepad.
  • Save it not as a TXT file but All Files.
  • Save it with .bat extension

Researching can also do wonders as knowledge of knowing the significance of such Variables will hinder the prevention of deletion. For this reason, it is best not to have elevated privileges to prevent accidental deletion that would ordinarily make your system more stable.

User-Defined Variables

User-defined environment variables are custom variables created by users to store values that can be used across terminal sessions, scripts, and applications. These variables enable personalization and customization of the system environment.

Common Use Cases

  • Storing frequently used paths, like project directories or tools.
  • Configuring applications or scripts to access specific settings (e.g., API keys or default language preferences).
  • Simplifying repetitive commands by referencing variables instead of hardcoded values.

Creation

  • In Windows you go in System Settings or use setx for persistent variables.
  • In Unix, use export in the terminal for temporary variables or add them to configuration files (e.g., .bashrc) for permanence.

Best Practices

  • Use meaningful names for clarity (e.g., PROJECT_DIR instead of VAR1).
  • Avoid storing sensitive data unless encrypted or securely handled.
  • Document added variables for easier management.
⚠️ **GitHub.com Fallback** ⚠️