Shell Script · Variables

Shell Scripting · Lesson 6

Variables

Understand variables in shell scripting. System and user defined variables. Rules for naming variables. Using variables in real automation scripts.

What Are Variables?

Variables are named containers used to store data in shell scripting. They allow scripts to save values and reuse them whenever needed.

A variable can store:

  • Text values
  • Numbers
  • Command output
  • File paths
  • User input

Shell variables are dynamically created and do not require a specific data type declaration.

Syntax:

variable_name=value

Important: Do not use spaces around the "=" operator.

Why Use Variables?

Store Data

Store values that can be reused throughout the script.

Avoid Repetition

Change a value once and reuse it everywhere.

Dynamic Behavior

Scripts behave dynamically based on variable values.

Types of Variables

Shell scripting mainly uses three types of variables:

Local Variables

Local variables are available only inside the current shell or script where they are created.

  • Accessible only in the current session/script.
  • Not shared with child processes.
  • Used for temporary data inside scripts.
terminal — bash
local-variable
name="Rahul"
course="Linux"
 
echo $name
echo $course

Environment Variables

Environment variables are shared with child processes and system programs.

  • Created using the export command.
  • Accessible by child shells and applications.
  • Common examples: PATH, HOME, USER.
terminal — bash
environment-variable
export CITY="Delhi"
 
echo $CITY
 
env | grep CITY

Read-only (Constant) Variables

Read-only variables cannot be modified after assignment.

  • Created using readonly.
  • Acts like a constant variable.
  • Useful for fixed values.
terminal — bash
readonly-variable
readonly PI=3.14
 
echo $PI
 
PI=3.1415
# Error: readonly variable

Common Variable Examples

1. Defining a Variable

Variables are created by assigning a value without spaces around '='.

terminal — bash
variables
name="Linux"
echo $name

2. Numeric Variable Example

Variables can store numbers and be used in calculations.

terminal — bash
variables
num=10
echo $num
 
sum=$((num + 5))
echo $sum

3. Updating a Variable

Variables can be reassigned with new values.

terminal — bash
variables
course="Shell"
echo $course
 
course="Linux"
echo $course

4. Store Command Output

Variables can also store the output of commands.

terminal — bash
variables
today=$(date)
echo $today

Accessing Variables

To access the value of a variable, use the $ symbol before the variable name.

terminal — bash
access-variable
language="Shell Scripting"
 
echo $language

Best Practices

Good Practices

  • Use meaningful variable names.
  • Avoid spaces around '='.
  • Use uppercase names for constants.
  • Quote variables to prevent word splitting.
  • Use export only when child processes need access.
  • Keep variable names simple and readable.

Common Mistakes

  • Using spaces around "=":name = "Linux"
  • Forgetting the $ while reading variable values.
  • Overwriting important environment variables accidentally.
  • Not quoting variables containing spaces.

Practice Task – Variables

  • Create a variable named name and print it.
  • Create a variable age and update its value.
  • Store the output of the date command inside a variable.
  • Create a constant variable using readonly.
  • Export a variable and verify it using env.

Next lesson: User Input (read command)