Skip to main content
In this lesson, you’ll learn how to view, customize, and persist environment variables on Linux. We cover session-level tweaks, system-wide settings, and automating tasks at login.

What Are Environment Variables?

Environment variables are key–value pairs that your shell and applications use to determine behavior, file locations, and settings. You can list all of them with either:
Example output:

Modifying Bash History Size

By default, HISTSIZE=1000 limits your Bash history to 1 000 commands. To increase it for the current session:
Verify:
Sample:

Common Environment Variables

Here are a few variables you’ll encounter frequently: To display a variable’s value:
Use these expansions for platform-independent scripting:
Each user running this command will create saved_file in their own home directory.

Setting Persistent Environment Variables

User-specific variables can go in ~/.bashrc or ~/.profile, but for system-wide settings, use /etc/environment.
Line-based syntax only—no shell expansions or functions.
Example entries look like KEY="value".

Edit /etc/environment

  1. Open the file with root privileges:
  2. Add your variable:
  3. Save and exit.
  4. Log out and back in, then verify:
Changes in /etc/environment affect all users and services.
Always back up /etc/environment before editing.

Automating Commands on Login

To execute commands for every user at login, place shell scripts in /etc/profile.d/. For instance, record the last login time:
  1. Create a script file:
  2. Add the following (no shebang required):
  3. Save and exit.
  4. Log out and back in, then check:
This script uses $HOME and demonstrates how to run tasks automatically upon user login.

Watch Video