🧩 2. Bash Basics & Syntax
Estimated reading: 3 minutes 66 views

🐚 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)

🧠 unset removes both local and exported variables.


🧠 Local vs Environment Variables

TypeVisibilitySet WithExample
Local VariableCurrent shell onlyvar=valuecount=5
Environment VariableCurrent shell + subprocessesexportexport 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
  • export shares variables with subprocesses
  • unset deletes 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Using Variables ($var, export, unset)

Or Copy Link

CONTENTS
Scroll to Top