Linux Variable Basics
Understanding Linux Variables
In Linux, variables are fundamental storage units that hold data for use in shell scripts and command-line operations. They serve as containers for storing information such as strings, numbers, and system configurations.
Types of Variables
Linux supports several types of variables:
Variable Type |
Description |
Example |
Local Variables |
Accessible only in current shell session |
name="LabEx" |
Environment Variables |
Available to all child processes |
PATH=/usr/local/bin |
Shell Variables |
Special variables managed by shell |
$HOME , $USER |
Variable Naming Conventions
graph TD
A[Variable Naming Rules] --> B[Start with letter or underscore]
A --> C[Contain letters, numbers, underscores]
A --> D[Case-sensitive]
Basic Variable Declaration
## Local variable
username="LabEx"
## Environment variable
export WORKSPACE="/home/user/projects"
Variable Scope and Accessibility
- Local variables are session-specific
- Environment variables persist across subprocess environments
- Shell variables provide system and shell-specific information
Variable Expansion
Variables can be referenced using $
prefix:
greeting="Welcome to LabEx"
echo $greeting ## Prints: Welcome to LabEx
By understanding these basics, users can effectively manage and utilize variables in Linux systems.