← Back to Bash Course | Chapter 2: Variables & Types | Lesson 1 of 13

Variable Basics

A variable is like a sticky-note label on a jar that holds something you want to remember. You write the name once and then use it whenever you need what is inside.

In this page:

  1. Variable Basics
Syntax
bash
variable_name=value
echo "$variable_name"

Variable Basics

Bash variables are assigned with name=value and no spaces around the =. You read a variable's value by prefixing it with $, like $name.

Bash has no static types — every variable is stored as a string internally, even when it holds something that looks numeric.

Warning: name = value (with spaces) is a syntax error — Bash interprets it as trying to run a command called name.

Example: Variable Basics

bash
#!/bin/bash
name="Alice"
age=30

echo "Name: $name"
echo "Age: $age"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting spaces around the equals sign, as in name = "Ada", which Bash reads as a command named name.
  2. Using $ on the left side, as in $name="Ada", which is an error.
  3. Forgetting $ when reading a variable, so echo name prints the word name.

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.