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:
Syntax
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
#!/bin/bash
name="Alice"
age=30
echo "Name: $name"
echo "Age: $age"
Login to try C/C++/Java/PHP code in the editor
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
- Putting spaces around the equals sign, as in
name = "Ada", which Bash reads as a command namedname. - Using
$on the left side, as in$name="Ada", which is an error. - Forgetting
$when reading a variable, soecho nameprints the wordname.
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: