Environment variables

2.6 Summarize scripting basics for server administration.

📘CompTIA Server+ (SK0-005) 


Definition:
Environment variables are dynamic values that the operating system and applications use to store information about the system environment. They are like little “settings” that tell scripts, programs, or the OS where to find certain files, how to behave, or store temporary information.

Think of them as named placeholders for important system information.


Why Environment Variables Are Important in Server Administration

  1. Simplify scripting: Scripts can use environment variables instead of hardcoding paths or settings.
  2. Provide flexibility: A single script can run on multiple servers without changes if environment variables are properly used.
  3. Standardize configuration: System-wide or user-specific settings can be stored centrally and referenced consistently.

Types of Environment Variables

  1. System Environment Variables
    • Defined for the entire server.
    • All users and scripts can access them.
    • Example: PATH variable tells the OS where to look for executable files like python or ping.
  2. User Environment Variables
    • Defined for a specific user account.
    • Only accessible by that user.
    • Example: TEMP or TMP folder location for temporary files.
  3. Process Environment Variables
    • Defined only for a running process.
    • Usually set in a script and disappear after the process ends.
    • Example: A script might set a variable LOG_PATH to store logs temporarily during execution.

Common Environment Variables in IT Environments

Variable NamePurposeExample Use in IT
PATHDirectories to search for executablesA script can run mysql or python without specifying full path
HOME / USERPROFILEUser’s home directoryScripts can save configuration files or logs in the user’s home folder
TEMP / TMPTemporary file storage locationScripts can store temporary installation files here
JAVA_HOMEPath to Java installationRequired by applications that need Java to run
SERVER_ENVCustom variable to define environment typeA script can check if the server is PRODUCTION or DEVELOPMENT before executing certain commands

How to View and Set Environment Variables

Windows

  • View variables: echo %PATH%
  • Set a temporary variable for the session: set LOG_PATH=C:\Logs
  • Set a permanent variable:
    1. Right-click This PC → Properties → Advanced System Settings → Environment Variables
    2. Add or modify a variable for user or system scope.

Linux / macOS

  • View variables: echo $PATH
    env
    printenv
  • Set a temporary variable: export LOG_PATH=/var/logs
  • Set a permanent variable:
    Add it to the user’s shell configuration file (like ~/.bashrc or ~/.bash_profile): export LOG_PATH=/var/logs

Using Environment Variables in Scripts

Example: Bash script using environment variables

#!/bin/bash
# Store log path in environment variable
export LOG_PATH="/var/logs/myapp"# Use the variable to save log
echo "Server started successfully" >> $LOG_PATH/server.log

Example: PowerShell script using environment variables

# Set a temporary environment variable
$env:LOG_PATH = "C:\Logs"# Write log to the variable path
"Server started successfully" | Out-File "$env:LOG_PATH\server.log"

Benefit: If the log folder changes, you only update the variable once instead of every script.


Best Practices for Environment Variables in Server Administration

  1. Use clear and descriptive names – avoid vague names like VAR1.
  2. Use environment variables for paths and configuration settings, not hardcoded values.
  3. Limit sensitive data exposure – do not store passwords in plain environment variables; use secure vaults instead.
  4. Check for existence – scripts should check if a variable exists before using it.

Key Exam Tips for CompTIA Server+

  • Understand what environment variables are and their role in scripts and system operations.
  • Know the difference between system, user, and process variables.
  • Be familiar with common environment variables: PATH, TEMP, HOME/USERPROFILE, JAVA_HOME.
  • Know how to view and set environment variables in Windows (echo %VAR%, setx) and Linux (echo $VAR, export).
  • Be able to write scripts that use environment variables for paths, logs, and configurations.

💡 Quick Memory Tip:
Environment variables = “server memory bookmarks” that scripts and programs can quickly reference to know where to go or what to use.

Buy Me a Coffee