๐ฆ Variables and Environment
Variables store values for reuse within scripts or sessions. They are essential building blocks for dynamic behavior.
๐งฎ Declaring and Using Variables
Assign value to variable:
1 2 | |
Access value using $ prefix:
1 2 | |
Enclose in braces for clarity or disambiguation:
1 | |
๐ Environment Variables
Environment variables are inherited by child processes:
1 2 | |
View all environment variables:
1 2 3 | |
Get specific one:
1 | |
Some commonly used ones: | Variable | Purpose | |----------|----------------------------| | HOME | User's home directory | | PATH | Directories searched for commands | | USER | Current username | | PWD | Present Working Directory |
๐งช Parameter Expansion
Shell supports advanced expansions:
1 2 3 4 5 6 | |
Pattern-based replacements:
1 2 | |
Length of string:
1 | |
๐งพ Summary
- Assign variables with
var=value. - Access them with
$varor${var}. - Export to make available to children.
- Use parameter expansion for transformations.
- Avoid spaces around
=in assignments.
๐ Continue to: Quoting