System Variables in ShellScript

System variables in ShellScript are predefined by the Linux Bash shell and play a vital role in managing environment configurations, script execution, and debugging processes. These variables are defined in uppercase letters and can be used to extract valuable information about the shell environment.

What are System Variables?

System variables are created and maintained by the Linux shell itself. These variables configure aspects of the shell's behavior and environment, such as:

  • User details
  • File paths
  • System locales
  • Shell configurations

Key Commands for Managing Variables

  • env: Displays the environment variables of the current session.
  • printenv: Prints all or specific environment variables.
  • set: Displays all shell variables, including functions.
  • unset: Removes a specified variable.
  • export: Makes a variable available for sub-shells.

Common System Variables and Use Cases

Below is a table of frequently used system variables along with their descriptions and practical use cases:

VariableDescriptionUse Case
USERCurrent logged-in userIdentify the user running the script: echo "Script executed by $USER"
HOMEHome directory of the current userUse as a base path for file operations: cd $HOME/Documents
SHELLPath of the user's current shellCheck if Bash is being used: if [[ $SHELL == "/bin/bash" ]]; then ...
HOSTNAMEName of the machineDisplay machine-specific messages: echo "Running on $HOSTNAME"
PWDPresent working directoryLog the current directory for debugging: echo "Current directory: $PWD"
UIDUser ID of the current userEnsure script runs as root: if [[ $UID -ne 0 ]]; then echo "Run as root"; fi
PATHDirectories searched for commandsAdd custom directory for scripts: export PATH=$PATH:/my/custom/scripts
OSTYPEType of the operating systemCheck OS compatibility: if [[ $OSTYPE == "linux-gnu" ]]; then ...
PPIDParent process IDDebug process hierarchy: echo "Parent process ID: $PPID"
SECONDSNumber of seconds since the script startedTime script execution: echo "Script ran for $SECONDS seconds"
RANDOMGenerate a random numberSimulate dice roll: echo $(( $RANDOM % 6 + 1 ))
LINESNumber of lines on the terminalOptimize display based on terminal size: echo "Terminal lines: $LINES"
COLUMNSNumber of columns on the terminalAdjust script output width: echo "Terminal width: $COLUMNS"
PS1Primary shell prompt stringCustomize shell prompt: PS1="MyShell> "
LANGCurrent locale settingsSwitch language dynamically: export LANG=fr_FR.UTF-8
LOGNAMEName of the current userDisplay the user performing critical actions: echo "User: $LOGNAME"
TMPDIRPath to the temporary directoryStore temporary files: temp_file="$TMPDIR/my_temp.txt"

Using System Variables in Shell Scripts

Below is an example of a script that demonstrates the use of system variables:

#!/bin/bash
# Script: system_variables.sh
# Purpose: Demonstrate system variables usage.

echo "User: $USER"
echo "Home Directory: $HOME"
echo "Shell: $SHELL"
echo "Machine Name: $HOSTNAME"
echo "Working Directory: $PWD"
echo "User ID: $UID"
echo "System Path: $PATH"
echo "Operating System: $OSTYPE"
echo "Parent Process ID: $PPID"
echo "Script Runtime: $SECONDS seconds"
echo "Random Number: $RANDOM"
echo "Terminal Dimensions: $LINES lines x $COLUMNS columns"
echo "Temporary Directory: $TMPDIR"

Output Example

User: gaurav
Home Directory: /home/gaurav
Shell: /bin/bash
Machine Name: learning-ocean
Working Directory: /home/gaurav/scripts
User ID: 1000
System Path: /usr/local/bin:/usr/bin:/bin
Operating System: linux-gnu
Parent Process ID: 12345
Script Runtime: 5 seconds
Random Number: 27659
Terminal Dimensions: 24 lines x 80 columns
Temporary Directory: /tmp

Best Practices

  • Use descriptive names for user-defined variables to avoid conflicts with system variables.
  • Validate paths (e.g., $HOME, $PATH) to ensure scripts run reliably across environments.
  • Avoid overwriting critical system variables unless absolutely necessary.

Demo Video

Click Here for Demo Video


By understanding and using system variables effectively, you can make your shell scripts more dynamic, adaptable, and robust. Experiment with these variables to explore their full potential in your projects!