Linux Intermediate 03 — Bash Variables, Quoting, and Input

Linux course index · Linux Intermediate category · Lesson 3 of 9

AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.

Learning goal

Write Bash variables, capture a command result, and accept input without losing spaces or misinterpreting nonnumeric input.

Concepts and setup

Use name=value with no spaces around the equals sign. Single quotes preserve literal text. Double quotes still allow expansions while keeping the result a single argument. read -r keeps backslashes and IFS= avoids trimming leading or trailing whitespace. Export a variable only when child processes need to see it.

This adapts the site’s Korn shell date example and its two-number Bash arithmetic example. The two-digit validation in this lab keeps arithmetic bounded and a decimal prefix prevents accidental octal interpretation.

Capture the date and preserve a value with spaces

day=$(date +%F)
printf 'Today is %s.\n' "$day"
name='weekly report'
printf '<%s>\n' "$name"
printf '%s\n' '$name'
export name
bash -c 'printf "child sees: %s\n" "$name"'

Possible output

Today is 2026-11-16.
<weekly report>
$name
child sees: weekly report

The date is illustrative. printf has a fixed format string and one quoted argument. The child process sees name because it was exported.

Build a decimal arithmetic script

lab=$(mktemp -d)
cd "$lab" || exit
cat > calc.sh <<'EOF'
#!/usr/bin/env bash
IFS= read -r a || exit 2
IFS= read -r b || exit 2
if ! [[ $a =~ ^[0-9]{1,2}$ && $b =~ ^[0-9]{1,2}$ ]]; then
  printf 'Enter two integers from 0 to 99\n' >&2
  exit 2
fi
a=$((10#$a))
b=$((10#$b))
printf 'sum=%d product=%d\n' "$((a+b))" "$((a*b))"
if (( b == 0 )); then
  printf 'division skipped: zero divisor\n'
else
  printf 'integer quotient=%d\n' "$((a/b))"
fi
EOF
printf '20\n05\n' | bash calc.sh
printf '8\n0\n' | bash calc.sh

Possible output

sum=25 product=100
integer quotient=4
sum=8 product=0
division skipped: zero divisor

The quoted heredoc sends the script verbatim to cat. Use Bash to run it because [[ ]] and the arithmetic details here are Bash-specific.

Questions only

Quick-check questions

  1. Why do we use read -r?
  2. What does double quoting "$name" protect against?

Assessment questions

  1. Predict the output with inputs 9 and 2. Why not 4.5?
  2. What should the script return when fed an empty field?
  3. Add a line that prints a decimal remainder without dividing by zero.

Questions and answers

Quick-check answers

  1. Why do we use read -r?
    Answer — It prevents read from treating backslashes as escapes.
  2. What does double quoting "$name" protect against?
    Answer — It keeps the expanded value as one argument and prevents pathname expansion on that value.

Assessment answer key

  1. Predict the output with inputs 9 and 2. Why not 4.5?
    Answer — sum=11 product=18 and integer quotient=4. Bash arithmetic here uses integer division.
  2. What should the script return when fed an empty field?
    Answer — 2 for invalid input or a missing input line.
  3. Add a line that prints a decimal remainder without dividing by zero.
    Answer — Inside the nonzero-divisor branch, add printf ‘remainder=%d\n’ "$((a%b))".

Source articles on this site

Technical references

Continue the course

Previous lesson 02 — Access Control Lists and Shared Directories. Next lesson 04 — Bash Conditions, Loops, Functions, and Debugging.

Linux course index · Linux Intermediate category · Lesson 3 of 9

Leave a Reply