🧩 2. Bash Basics & Syntax
Estimated reading: 3 minutes 384 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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top