Skip to content
Learn Netverks

Lesson

Step 7/36 19% through track

variables-bash

Variables in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Variables in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Variables in Bash in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Variables in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

Bash variables hold strings. Assignment has no spaces around =. Use $name or ${name} to read values—compare with Python's clearer assignment rules.

Assignment and echo

name="Ada"
count=3
echo "Hello, $name"
echo "count is ${count}"

Quotes prevent word-splitting on spaces. Without quotes, multiple words become multiple arguments.

Read-only and export

readonly PI=3.14
export APP_ENV=dev
echo "$APP_ENV"

export puts variables into the environment child processes inherit—covered more in env-vars lesson.

Command substitution

today=$(date +%F)
echo "Today is $today"
lines=$(wc -l < notes.txt)
echo "notes.txt has $lines lines"

$(...) runs a command and captures stdout—like running a function that returns text.

Important interview questions and answers

  1. Q: Spaces around = in assignment?
    A: Forbidden for simple assignment—name=Ada tries to run command Ada.
  2. Q: $name vs ${name}?
    A: Braces disambiguate names: ${file}_backup vs $file_backup.

Self-check

  1. Why is name = Ada invalid?
  2. What syntax runs date and stores its output?

Pitfall: No spaces around = in assignments—count = 3 runs command =.

Interview prep

Assignment spacing rule?

No spaces around = in name=value assignments.

Command substitution syntax?

$(command) captures stdout as text.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • export when?
  • readonly use?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump