Black Friday Hosting Deals: 69% Off + Free Migration: Grab the Deal Grab It Now!
Environment variables are an essential component of Linux and any UNIX-based operating system. They store information about the shell session and system configuration that can be accessed by applications and processes running on the system.
When working on a Linux VPS, knowing how to read and modify environment variables is crucial for administering and configuring your server environment effectively. This guide will explain what environment variables are, how to view, set and unset variables using common Linux commands, and some key environment variables worth knowing on a Linux VPS.
Environment variables are named values stored within the system that contain information related to the shell session, user preferences, and system configuration.
Some common examples include:
$HOME - The home directory path for the current user
$PATH - Directories where executable programs are located
$USER - Current username
Shell scripts and applications can access these values during execution to alter behavior based on the current environment. Environment variables enable processes to be configured independently without hard coding values.
When a new shell session starts, it gathers and assembles information to make available for that shell process and any child processes it creates. The shell obtains this data from various system files and configurations that define the environment variables for that session.
The environment provides a medium through which the shell process can get or set settings and, in turn, pass these on to its child processes.
Environment variables are stored as key-value string pairs. For variables with multiple values, these are typically separated by colon (:) characters. An environment variable pair generally follows this format:
KEY=value1:value2:...
If the value contains significant white-space, quotations are used:
KEY="value with spaces"
The keys in these pairs are variables, which can be either environmental or shell variables:
Environmental variables are defined for the current shell and inherited by any child processes spawned from it. They pass information into launched processes.
Shell variables are exclusive to the current shell and not propagated. They often track ephemeral data like working directory.
By convention, environmental and shell variables are usually named with capital letters to distinguish them in code.
There are several ways to view environment variables and their current values on a Linux VPS:
printenv
printenv
env
set
Display shell variables and functions as well as environment variables.
export
To set a new environment variable, use the following syntax:
export VAR_NAME="value"
For example:
export APP_DIR="/opt/myapp"
Sets APP_DIR to /opt/myapp.
To permanently set a variable, add the export line to:
/etc/environment - System-wide variable available to all users/processes
~/.bashrc - User variable available when that user logs in
/etc/profile - System-wide defaults for bash login shells
Unsetting Variables
To unset an environment variable, use the unset command:
unset VAR_NAME
Unsets the APP_DIR variable.
Key Environment Variables on Linux VPS
Below are some key environment variables that are good to be familiar with when administrating a Linux VPS:
$HOME - The current user's home directory. Useful for default file paths.
$PWD - The current working directory. Helps reference files relative to the current location.
$PATH - Directories where executables are stored. Used by shells when running commands.
$USER - Current username. Used in shell prompts by default.
$SHELL - Current shell being used such as bash or sh.
$LANG - System language settings. Important for software localization.
$TCP_RANDOM_PORT_NEG - Controls random port assignment for TCP sockets. Useful for configuring services.
$HOSTNAME - Hostname of the system. Used for identification.
$TERM - Terminal emulator in use such as xterm or ssh.
$EDITOR - Default text editor. Used by applications needing text editing capabilities.
Knowing these common variables will help you reference important system information within scripts and configurations.
Modifying Startup Environment
To persistently set environment variables that are initialized at system startup, modify these files:
/etc/environment - Sets system-wide environment variables
~/.bash_profile or ~/.profile - Set user-specific environment defaults
/etc/profile - System-wide defaults for all user login shells
Make sure to restart the system or login shell to reload updated environment configurations.
Environment variables are an essential component of Linux and UNIX-based systems that enable the abstraction of configuration details from the underlying system and applications. Learning to properly read, set, and unset environment variables from the command line or startup scripts is a key skill for effectively administering a Linux VPS.
Use printenv, env, set and export to view variables
Set variables with export VAR=value
Unset variables using unset
Modify startup files to persist variables
Reference key variables like HOME, PATH, SHELL etc.
With this knowledge, you can leverage environment variables to fully optimize and customize your Linux VPS environment. They allow application and system configurations to be set independently for simplicity and flexibility.
Let’s talk about the future, and make it happen!
By continuing to use and navigate this website, you are agreeing to the use of cookies.
Find out more