Lesson 1 · Unit 04
Variables and quoting
name=value, the two kinds of quotes, $(command), $1 and $#
What you practise
- The shebang,
$varvs"$var"vs'$var',$(command),$1,$@,$#, arithmetic with$(( ))
Try a question from this lesson
Pick one
A variable is set with name=value and read with $name. The two echo lines differ only in their quotes. What prints?
name="Ada"
echo 'Hi $name'
echo "Hi $name"Correct answer
Hi $name, then Hi Ada
Why
Single quotes keep every character exactly as typed, so $name stays as it is. Double quotes still let the shell swap in the value. Use double quotes unless you want a literal dollar sign.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A script
A script is a file of commands. Its first line, the shebang, tells the kernel which program should run it.
name=value sets a variable: no spaces around the equals sign. $name reads it.
Give the file the execute bit with chmod +x and run it as ./greet.sh.
#!/bin/bash
# greet.sh: say hello
name="world"
echo "Hello, $name"Concept
Arguments, and why to quote them
A script reads its arguments by position: $1, $2 and so on. $# is how many there are, $@ is all of them, and $0 is the script's own name.
Put double quotes around every use. Unquoted, a value is split at its spaces into several words, and an empty value disappears altogether.
echo "script: $0"
echo "first: $1"
echo "count: $#"
cp "$1" "$2" # safe with spaces9 more exercises in the app
- 1× true or false
- 3× type the answer
- 1× fill the blanks
- 2× multiple choice
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.