Querying Techniques
Basic Querying Methods
1. Using printenv
Command
The printenv
command displays all current environment variables:
## Display all environment variables
printenv
## Display specific variable
printenv HOME
2. Using env
Command
## List all environment variables
env
## Filter specific variables
env | grep USER
Advanced Querying Techniques
3. Bash Built-in Methods
## Using echo with variable expansion
echo $PATH
echo $HOME
echo $USER
## Using set command to list all variables
set
graph LR
A[Querying Techniques] --> B[printenv]
A --> C[env]
A --> D[echo $variable]
A --> E[set command]
Variable Querying Comparison
Method |
Scope |
Usage |
Performance |
printenv |
Global |
Full variable list |
Moderate |
env |
Global |
Filtered output |
Fast |
echo $VAR |
Specific |
Single variable |
Fastest |
set |
Shell-specific |
Detailed listing |
Comprehensive |
Special Querying Techniques
4. Using declare
Command
## List all variables with declare
declare
## List only environment variables
declare -x
LabEx Pro Tip
In LabEx Linux environments, combine these techniques to efficiently explore and manage system variables across different contexts.
5. Conditional Variable Checking
## Check if variable exists
if [ -z "$VARIABLE" ]; then
echo "Variable not set"
else
echo "Variable is set"
fi