Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

arguments-bash

Script arguments

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

This lesson

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

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

You will apply Script arguments 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.

Positional parameters pass data into scripts: $0 is the script name, $1… are args, $# is the count, $@ expands to all args.

Positional parameters

#!/usr/bin/env bash
echo "script: $0"
echo "args: $@"
echo "count: $#"
echo "first: ${1:-none}"

${1:-none} uses default none if $1 is unset—part of parameter expansion.

shift

while [[ $# -gt 0 ]]; do
  echo "arg: $1"
  shift
done

shift consumes arguments—common for subcommands like tool build / tool test.

getopts

verbose=0
while getopts "v" opt; do
  case $opt in
    v) verbose=1 ;;
  esac
done
shift $((OPTIND - 1))
echo "verbose=$verbose rest=$@"

getopts parses short flags; long options need manual parsing or external tools.

Important interview questions and answers

  1. Q: $@ vs $*?
    A: $@ preserves separate words; $* joins into one string—prefer "$@" when forwarding args.
  2. Q: What is $0?
    A: Script name or path used to invoke the script.

Self-check

  1. Which variable holds argument count?
  2. What does shift do?

Tip: Forward args with "$@", not $*, when wrapping commands.

Interview prep

$#?

Number of positional arguments.

shift?

Removes $1 and shifts others down.

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

  • $1 vs $*?
  • shift loop?

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