Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

parameter-expansion-bash

Parameter expansion

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

This lesson

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

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

You will apply Parameter expansion 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.

Parameter expansion manipulates strings in variable values—defaults, substring, and pattern removal—without calling external tools.

Defaults and assign-default

echo "${NAME:-guest}"
: "${COUNT:=0}"
echo "COUNT=$COUNT"

:= assigns if unset or null; :- only substitutes for the expansion.

Substring and length

file="report-2024-05.txt"
echo "${file:0:7}"
echo "len=${#file}"

${var:offset:length} slices strings—handy for parsing fixed-width names.

Pattern removal

path="/var/www/html/index.html"
echo "${path##*/}"    # basename
echo "${path%/*}"     # dirname style

## longest match from front; % shortest from end—powerful but read carefully.

Important interview questions and answers

  1. Q: ${var:-default} vs ${var:=default}?
    A: :- substitutes; := also assigns default to var when unset/null.
  2. Q: What does ##*/ remove?
    A: Longest prefix matching */ leaving the filename.

Self-check

  1. Which expansion provides a default without assignment?
  2. What operator gives string length?

Tip: ${var:-default} avoids unset surprises—common in deploy scripts.

Interview prep

${var:-def}?

Use default if unset/null without assignment.

${var##*/}?

Remove longest prefix matching */ (basename style).

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

  • ${var:-default}?
  • Trim suffix?

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