In this chapter, we’ll cover:
- What environment variables are and why they matter.
- How to view and set environment variables.
- Customizing your shell environment using configuration files like .bashrc.
Environment variables are key to configuring how your shell and programs behave. They control everything from your command prompt to the paths where programs look for files. Understanding them allows you to customize your system and automate tasks more effectively.
Environment variables are named values that store information about the environment in which processes run. They can affect the behavior of programs and scripts.
HOME: Your home directory.PATH: A list of directories where the shell looks for commands.USER: Your username.SHELL: The path to your current shell.You can view environment variables using the following commands.
printenv: Print All or Specific Variables$ printenv$ printenv HOMEecho: Print a Specific VariableUse echo with a $ prefix to print the value of a variable.
$ echo $HOME
/home/userYou can set environment variables temporarily or permanently.
Set a variable for the current session:
$ MY_VAR="Hello, Linux!"
$ echo $MY_VAR
Hello, Linux!To make a variable available to child processes, use export:
$ export MY_VAR="Hello, Linux!"To make a variable persistent across sessions, add it to your shell configuration file (e.g., .bashrc):
$ echo 'export MY_VAR="Hello, Linux!"' >> ~/.bashrc
$ source ~/.bashrcPATH VariableThe PATH variable is one of the most important environment variables. It tells the shell where to look for executable files.
PATH$ echo $PATH
/usr/local/bin:/usr/bin:/binPATHTo add a directory to the PATH:
$ export PATH=$PATH:/new/directoryFor permanent changes, add the line to your .bashrc:
$ echo 'export PATH=$PATH:/new/directory' >> ~/.bashrc
$ source ~/.bashrcShell configuration files allow you to customize your environment. The most common file is .bashrc.
.bashrcThis file is executed whenever you start a new shell. It’s a great place to set environment variables, aliases, and functions.
.bashrc: $ nano ~/.bashrc.bashrc: $ source ~/.bashrc.bash_profile: Executed for login shells..profile: Similar to .bash_profile, but not specific to Bash.You can customize your shell prompt by modifying the PS1 variable.
Add the following line to your .bashrc:
$ export PS1="\u@\h [\t] \w\$ "This sets the prompt to:
$ user@debian [12:34:56] ~$PS1 Codes\u: Username.\h: Hostname.\t: Time (HH:MM:SS).\w: Current working directory.Environment variables are often used in scripts to store configuration values or pass information between processes.
#!/bin/bash
echo "Hello, $USER!"
echo "Your home directory is $HOME."Use .env files for managing environment variables:
$ source .envLoad environment variables with dotenv:
$ dotenvManaging environment variables efficiently is key to configuring your shell and applications. Using .env or .envrc files to store variables allows for easier management and portability across projects. Tools like dotenv or direnv can automate the process of loading these variables when entering a project directory. When setting system-wide environment variables, caution is advised to avoid conflicts or unexpected issues. A well-organized environment ensures consistent behavior and fosters reusability across different contexts.
# Example usage:
echo "VAR_NAME=value" >> .env
source .env
dotenv
direnv allowLet’s put your new skills to the test:
1. Use printenv to view all environment variables.
2. Set a temporary environment variable and print its value.
3. Add a directory to your PATH and test it by running a command from that directory.
4. Customize your shell prompt using the PS1 variable.
That’s it for Chapter 11! You’ve now learned how to work with environment variables and customize your shell environment. In the next chapter, we’ll dive into the command-line environment—exploring shell prompts, aliases, and shell customization. Until then, experiment with environment variables to make your Linux experience more personalized.