← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 11 of 12

Introduction to Variables

A variable is a named box that stores a piece of text or a number so your script can refer back to it later by name.

Assigning and Reading a Variable

You assign a variable with name=value, with no spaces around the =. To read the value back you prefix the name with a dollar sign, $name, which Bash expands (substitutes) with the stored text before running the command.

Warning: name = "Ada" (with spaces) fails because Bash tries to execute a command called name with arguments = and "Ada".

Example: Assigning and Reading a Variable

bash
#!/bin/bash
name="Ada Lovelace"
echo "Hello, $name!"

Using Braces: ${name}

Wrapping a variable name in braces, ${name}, makes the boundary of the variable name explicit, which matters when you want to immediately follow it with other text with no separator. Without braces, Bash would try to include that following text as part of the variable name.

Example: Using Braces: ${name}

bash
#!/bin/bash
fruit="apple"
echo "I have one ${fruit}s"
# echo "I have one $fruits" would look for a variable named 'fruits' instead

Reassigning and Building on Variables

Variables can be reassigned at any point in a script, and a variable's current value can be used while constructing its new value. This is common for building up strings or counters across several steps.

Example: Reassigning and Building on Variables

bash
#!/bin/bash
greeting="Hello"
greeting="$greeting, world"
echo "$greeting"
count=1
count=$((count + 1))
echo "Count is now $count"

Everything Is a String by Default

Bash does not have distinct numeric or boolean variable types out of the box -- every variable holds text, and Bash reinterprets that text as a number only in numeric contexts like arithmetic expressions. This is why age="5" and age=5 behave identically when stored.

Example: Everything Is a String by Default

bash
#!/bin/bash
age="5"
echo "Stored value: $age"
echo "Type as text length: ${#age} character(s)"
Common Mistakes
  1. Putting spaces around the = sign, like name = "Ada"; Bash requires no spaces (name="Ada"), otherwise it tries to run name as a command.
  2. Forgetting the $ when reading a variable's value; name refers to nothing/a command, while $name retrieves the stored value.
  3. Expecting variables to be typed like in other languages; by default every Bash variable is stored as a string, even if it looks like a number.
Chapter Summary
  • Assign with name=value (no spaces around =); read with $name or ${name}.
  • Variable names are case-sensitive and conventionally use lowercase for script-local variables, uppercase for exported/environment variables.
  • ${name} (with braces) is safer than $name when the variable name is immediately followed by other characters, e.g. ${name}s.
  • Unset variables expand to an empty string by default rather than causing an error (unless set -u is used).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.