Environment variables are key-value pairs that store information about the system environment. In Linux scripting, several built-in environment variables provide useful information and configuration settings. This tutorial will guide you through the usage of some common built-in environment variables in a Linux environment.
The $HOME
variable stores the path to the user's home directory.
echo "Your home directory is: $HOME"
This will display the path to your home directory.
These variables store the name of the current user.
echo "Current user: $USER"
echo "Login name: $LOGNAME"
These will display the current user's name and login name.
The $PWD
variable stores the current working directory.
echo "Current working directory: $PWD"
This will display the path to the current working directory.
The $PATH
variable contains a colon-separated list of directories where the system looks for executable files.
echo "Current PATH: $PATH"
This will display the directories included in the current $PATH
.
The $SHELL
variable stores the path to the user's default shell.
echo "Your default shell: $SHELL"
This will display the path to your default shell.
The $TERM
variable contains the type of terminal being used.
echo "Terminal type: $TERM"
This will display the type of terminal.
The $HOSTNAME
variable contains the name of the host (computer) in the network.
echo "Hostname: $HOSTNAME"
This will display the hostname of the system.
These variables store the user's real and effective user IDs, respectively.
echo "Real User ID: $UID"
echo "Effective User ID: $EUID"
These will display the real and effective user IDs.
The $RANDOM
variable generates a random integer between 0 and 32767.
echo "Random number: $RANDOM"
This will display a random number.
Built-in environment variables in Linux scripting provide valuable information about the system configuration and the user environment. Understanding and utilizing these variables can enhance the functionality and flexibility of your scripts. Experiment with these variables to tailor your scripts to specific system contexts and user environments.