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:
Variable | Description | Use Case |
---|---|---|
USER | Current logged-in user | Identify the user running the script: echo "Script executed by $USER" |
HOME | Home directory of the current user | Use as a base path for file operations: cd $HOME/Documents |
SHELL | Path of the user's current shell | Check if Bash is being used: if [[ $SHELL == "/bin/bash" ]]; then ... |
HOSTNAME | Name of the machine | Display machine-specific messages: echo "Running on $HOSTNAME" |
PWD | Present working directory | Log the current directory for debugging: echo "Current directory: $PWD" |
UID | User ID of the current user | Ensure script runs as root: if [[ $UID -ne 0 ]]; then echo "Run as root"; fi |
PATH | Directories searched for commands | Add custom directory for scripts: export PATH=$PATH:/my/custom/scripts |
OSTYPE | Type of the operating system | Check OS compatibility: if [[ $OSTYPE == "linux-gnu" ]]; then ... |
PPID | Parent process ID | Debug process hierarchy: echo "Parent process ID: $PPID" |
SECONDS | Number of seconds since the script started | Time script execution: echo "Script ran for $SECONDS seconds" |
RANDOM | Generate a random number | Simulate dice roll: echo $(( $RANDOM % 6 + 1 )) |
LINES | Number of lines on the terminal | Optimize display based on terminal size: echo "Terminal lines: $LINES" |
COLUMNS | Number of columns on the terminal | Adjust script output width: echo "Terminal width: $COLUMNS" |
PS1 | Primary shell prompt string | Customize shell prompt: PS1="MyShell> " |
LANG | Current locale settings | Switch language dynamically: export LANG=fr_FR.UTF-8 |
LOGNAME | Name of the current user | Display the user performing critical actions: echo "User: $LOGNAME" |
TMPDIR | Path to the temporary directory | Store 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
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!