Day 2: Variables, Quoting, Parameter Expansion
Goal: Set variables and pass them on, understand what the shell does with a line before a command starts, quote correctly, and take text apart with parameter expansion. At the end,
notes-addcreates notes. · Duration: approx. 75 min · Prerequisite: Day 1
Briefing
name=valuesets a variable – without spaces around=.$nameor${name}reads it.exportpasses it on to programs you start.- Before every command, the shell takes the line apart: it replaces
variables, splits the result into words at spaces (word
splitting) and inserts file names for
*(globbing). Double quotes prevent both. - Parameter expansion (
${…}) can do more than read: default values, length, cutting off parts, replacing. - Rule of thumb: put every variable in double quotes, unless you explicitly want splitting or globbing.
line ─► replace variables ─► word splitting ─► globbing ─► command starts
"$x" protects against both ──┘ └── '…' replaces nothing at all
Drill 1: Variables and export
source ~/sh-lab/setup/env.sh
mkdir -p ~/sh-lab/tag02 && cd ~/sh-lab/tag02
titel="Meine Notiz"
echo "$titel"
titel = "falsch" 2>&1 | head -1
bash -c 'echo "Kind sieht: ${titel:-nichts}"'
export titel
bash -c 'echo "Kind sieht: ${titel:-nichts}"'
FARBE=blau bash -c 'echo "nur für diesen Aufruf: $FARBE"'; echo "danach: ${FARBE:-leer}"Expected:
Meine Notiz
titel: command not found
Kind sieht: nichts
Kind sieht: Meine Notiz
nur für diesen Aufruf: blau
danach: leer
titel = "falsch"starts the commandtitelwith the arguments=andfalsch.- A variable without
exportlives only in your shell. Scripts and programs that you start don’t see it – that is whyenv.shsets everything withexport. ${titel:-nichts}returnsnichtsiftitelis empty or unset.
Save your progress? Sign up for free
Drill 2: Word splitting and globbing
A small helper script shows which arguments a command really receives:
cat > ~/sh-lab/werkzeug/bin/args <<'EOF'
#!/usr/bin/env bash
# args – shows each argument in square brackets
set -euo pipefail
echo "$# Argumente:"
for a in "$@"; do printf ' [%s]\n' "$a"; done
EOF
chmod +x ~/sh-lab/werkzeug/bin/args
text="zwei Wörter"
args $text
args "$text"
args '$text'Expected:
2 Argumente:
[zwei]
[Wörter]
1 Argumente:
[zwei Wörter]
1 Argumente:
[$text]
Without quotes, the shell splits the content at spaces – and the three spaces are gone. Double quotes preserve the value, single quotes even prevent the replacement. And globbing:
touch a.txt b.txt
stern="*.txt"
args $stern
args "$stern"
leer="*.gibtsnicht"
args $leerExpected:
2 Argumente:
[a.txt]
[b.txt]
1 Argumente:
[*.txt]
1 Argumente:
[*.gibtsnicht]
If a pattern matches no file, it stays as it is. A script that calls
rm $muster would then get a file name with an asterisk.
Save your progress? Sign up for free
Drill 3: A script’s arguments
cat > zeige <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
echo "Skript: $0, Anzahl: $#, erstes: ${1:-}"
echo "--- \$@ in Anführungszeichen:"; args "$@"
echo "--- \$* in Anführungszeichen:"; args "$*"
EOF
bash zeige "erste Notiz" zweiteExpected:
Skript: zeige, Anzahl: 2, erstes: erste Notiz
--- $@ in Anführungszeichen:
2 Argumente:
[erste Notiz]
[zweite]
--- $* in Anführungszeichen:
1 Argumente:
[erste Notiz zweite]
| Expression | Meaning |
|---|---|
$0 |
Name of the script |
$1 … $9, ${10} |
individual arguments |
$# |
count |
"$@" |
all arguments, each on its own – almost always the right choice |
"$*" |
all arguments as one text, separated by spaces |
${1:-} instead of $1: with
set -u, the script would otherwise abort when there is no
argument.
Save your progress? Sign up for free
Drill 4: Parameter expansion
url="http://127.0.0.1:8080/notes?limit=5"
datei="/var/backups/notes-2026-11-02.sql.gz"
echo "Länge: ${#url}"
echo "ohne Schema: ${url#*://}"
echo "nur Pfad: /${url#*://*/}"
echo "ohne Query: ${url%%\?*}"
echo "Dateiname: ${datei##*/}"
echo "Ordner: ${datei%/*}"
echo "ohne .gz: ${datei%.gz}"
echo "Stamm: $(basename "${datei%%.*}")"
echo "ersetzt: ${datei//-/_}"
echo "groß: ${USER^^}"
echo "Teilstück: ${datei:13:5}"Expected:
Länge: 35
ohne Schema: 127.0.0.1:8080/notes?limit=5
nur Pfad: /notes?limit=5
ohne Query: http://127.0.0.1:8080/notes
Dateiname: notes-2026-11-02.sql.gz
Ordner: /var/backups
ohne .gz: /var/backups/notes-2026-11-02.sql
Stamm: notes-2026-11-02
ersetzt: /var/backups/notes_2026_11_02.sql.gz
groß: UBUNTU
Teilstück: notes
Memory aid: on the keyboard, # sits to the left of
% – # cuts off at the front,
% at the back. Single (#,
%) takes the shortest matching piece, double
(##, %%) the longest. All of this runs in the
shell itself, without starting a program like sed.
POSIX:
${#x},${x#…},${x%…}and${x:-…}are portable. Replacing (${x//a/b}), case conversion (${x^^}) and substrings (${x:13:5}) are Bash extensions.
Save your progress? Sign up for free
Drill 5: notes-add
cat > ~/sh-lab/werkzeug/bin/notes-add <<'EOF'
#!/usr/bin/env bash
# notes-add – creates a note: notes-add text of the note
set -euo pipefail
text="$*"
curl -fsS -X POST -H "X-API-Key: $NOTES_API_KEY" -H 'Content-Type: application/json' \
-d "{\"text\":\"$text\"}" "$NOTES_URL/notes"
echo
EOF
chmod +x ~/sh-lab/werkzeug/bin/notes-add
shellcheck ~/sh-lab/werkzeug/bin/notes-add && echo sauber
notes-add Einkaufen: Milch und Brot
notes-add "Einkaufen: Milch und Brot"
notes-add 'Er sagte "hallo"'; echo "Exit: $?"Expected:
sauber
{"id":"…","text":"Einkaufen: Milch und Brot"}
{"id":"…","text":"Einkaufen: Milch und Brot"}
curl: (22) The requested URL returned error: 400
Exit: 22
- Without quotes in the call, your shell splits the
text into words, and
$*joins them back together with one space each. - The
"in the text ends the JSON string too early, and notes-api rejects the request. Building text into JSON is a job for a tool that understands JSON –jqon Day 8. - Inside double quotes,
\"protects a quote character.
Save your progress? Sign up for free
Checkpoint
notes-add "Kontrollpunkt Tag 2" | grep -o '"text":"[^"]*"'Expected:
"text":"Kontrollpunkt Tag 2"
args "a b" cExpected:
2 Argumente:
[a b]
[c]
f=/tmp/bericht.final.txt; echo "${f##*/} ${f%.txt} ${f%%.*}"Expected:
bericht.final.txt /tmp/bericht.final /tmp/bericht
Challenge
notes-add should work without env.sh when
notes runs at the default address http://127.0.0.1:8080. If
the API key is missing, however, it should abort with a clear message
before curl starts.
Note: ${VAR:-default} and
${VAR:?message}.
Solution
cat > ~/sh-lab/werkzeug/bin/notes-add <<'EOF'
#!/usr/bin/env bash
# notes-add – creates a note: notes-add text of the note
set -euo pipefail
url="${NOTES_URL:-http://127.0.0.1:8080}"
key="${NOTES_API_KEY:?fehlt – source ~/sh-lab/setup/env.sh}"
text="$*"
curl -fsS -X POST -H "X-API-Key: $key" -H 'Content-Type: application/json' \
-d "{\"text\":\"$text\"}" "$url/notes"
echo
EOF
shellcheck ~/sh-lab/werkzeug/bin/notes-add && echo sauber
env -u NOTES_URL notes-add "ohne env.sh-URL" | grep -c '"id"'
env -u NOTES_API_KEY notes-add test; echo "Exit: $?"Expected:
sauber
1
/home/ubuntu/sh-lab/werkzeug/bin/notes-add: line 5: NOTES_API_KEY: fehlt – source …
Exit: 1
env -u NAME starts a command without that variable –
ideal for testing things like this. The message names the script, line
and variable; you decide the rest.
Save your progress? Sign up for free
Enterprise Note
- Configuration via environment variables with
default values (
${X:-…}) and required values (${X:?…}) is the usual way – just like with containers. - Secrets not as arguments: Whatever is on the
command line, every user can see with
ps. Keys come from variables or files, never as--key geheim. - File names with spaces always exist, on a network drive at the latest. Scripts that only work without them are broken, not the file names.