π Bash Variables β Using $var, export, and unset in Shell Scripts
π§² Introduction to Bash Variables β $var, export, and unset Explained
In Bash scripting, variables are a fundamental part of storing, modifying, and passing data throughout a session or script. Whether you’re writing a small script or managing system-wide environment variables, mastering how to declare ($var), export, and unset variables will enhance your control over the Linux shell environment.
π― In this article, youβll learn:
- How to define and use variables in Bash
- The difference between local and environment variables
- How to export variables to child processes
- How to delete variables with
unset - Best practices and syntax pitfalls
π‘ What Are Variables in Bash?
A variable in Bash is a name that points to a value (usually a string). Bash doesn’t require explicit data typesβeverything is treated as text unless evaluated.
π€ Basic Variable Assignment
name="Alice"
echo $name
β Output:
Alice
π§ No spaces around the equal sign (
=) in Bash variable assignment!
π§ͺ Examples β Declare, Access, and Use Variables
π Define and Echo a Variable
greeting="Hello"
echo "$greeting, World!"
π Quotes Matter!
message="Welcome to Bash"
echo $message # Safe if no spaces
echo "$message" # Best practice
π Avoid Common Mistake
# β Incorrect (with space)
title = "Developer" # Syntax Error
π Exporting Variables with export
The export command makes a variable available to child processes, like subprocesses or other scripts.
β Example:
name="Vaibhav"
export name
π Check Exported Variables:
printenv name
# Output: Vaibhav
β οΈ Variables not exported are only accessible within the current shell/session.
π§Ή Deleting Variables with unset
Use unset to remove a variable from the shell environment.
β Example:
age=30
unset age
echo $age
# Output: (blank)
π§
unsetremoves both local and exported variables.
π§ Local vs Environment Variables
| Type | Visibility | Set With | Example |
|---|---|---|---|
| Local Variable | Current shell only | var=value | count=5 |
| Environment Variable | Current shell + subprocesses | export | export path="/bin" |
π Variable Naming Rules in Bash
- Must start with a letter or underscore
- Can contain letters, digits, and underscores
- Cannot include spaces or special characters like
- - Are case-sensitive
β Valid Examples:
my_name="bash"
_user="root"
MAX_LIMIT=100
β Invalid Examples:
2var=5 # starts with digit
my-var=123 # contains hyphen
π Temporary vs Permanent Environment Variables
π Temporary Environment Variables (Current Session Only)
export TEMP_VAR="session"
π Permanent Environment Variables (System/User Level)
Add to:
~/.bashrc~/.bash_profile/etc/environment(system-wide)
Example:
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk"
Then reload:
source ~/.bashrc
π Summary β Bash Variables
Bash variables give scripts and sessions flexibility and dynamic behavior. From simple string assignments to exporting environment variables, mastering $var, export, and unset is crucial for scripting and system automation.
π Key Takeaways:
- Variables are declared with
var=value, accessed with$var exportshares variables with subprocessesunsetdeletes any variable from the session- Use quotes to handle spaces and avoid syntax errors
βοΈ Real-world Uses:
- Set paths (
PATH,JAVA_HOME) dynamically - Use variables in loops and conditions
- Pass configuration values into scripts
β FAQ β Bash Variables
β What does $var mean in Bash?
β
$var is used to access the value of a variable. For example:
name="Linux"
echo $name # Output: Linux
β How do I export a variable for use in a script?
β
Use:
export MY_VAR="somevalue"
Then the variable is accessible to any child shell or script.
β How do I permanently set a variable in Bash?
β
Add the variable to your ~/.bashrc or ~/.bash_profile and reload it:
source ~/.bashrc
β What is the difference between unset and setting a variable to an empty string?
β
unset varname removes the variable, while varname="" just clears its content but keeps it defined.
β Can I use numbers in Bash variable names?
β
Yes, but not at the beginning. Example:
user1="admin" # β
valid
1user="admin" # β invalid
Share Now :
