Typorithm · easy

Variables & echo

bash-variablesbashvariableechoexpansion
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Variables & echo

0%

Basics

Assign variables with name=value (no spaces around =), expand them with $name or ${name}, run commands inline with $(...), make constants with readonly, and share variables with child processes via export. Quote values to keep spaces.

A bash variable is set with name=value and read back with $name. The assignment must have no spaces around the = sign — name="Ada" works, name = "Ada" does not.

Inside double quotes, $name (or the explicit ${name} form) expands to the value, while $(...) is command substitution: it runs the command and drops its output in place, like $(date +%F) for today's date.

readonly turns a variable into a constant that cannot be reassigned, and export marks a variable so child processes inherit it in their environment. Always quote values that may contain spaces.

01

name=value — no spaces around the = sign.

02

$name / ${name} expand a variable; $(...) substitutes command output.

03

readonly makes a constant; export shares a var with child processes.

name="Ada"
echo "Hi $name"
echo "Today: $(date +%F)"
readonly PI=3.14
export PATH="$HOME/bin:$PATH"

Read the implementation

Connect each part of the code to the concept before you type.

1. Assignment

Assign with no spaces around = — name="Ada", never name = "Ada".

name="Ada"

2. Variable expansion

$name (or ${name}) expands to the variable's value inside double quotes.

echo "Hi $name"

3. Command substitution

$(...) runs a command and substitutes its output into the line.

$(date +%F)

4. Constant

readonly makes a variable a constant that can no longer be reassigned.

readonly PI=3.14

5. Export to environment

export puts a variable in the environment so child processes inherit it.

export PATH="$HOME/bin:$PATH"

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Variables & echo: Lesson & Practice · Typorithm | ApplyDjinn