Bitpath

Lesson 1 · Unit 04

Variables and quoting

name=value, the two kinds of quotes, $(command), $1 and $#

12 exercisesabout 3 min

What you practise

  • The shebang, $var vs "$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?

shell
name="Ada"
echo 'Hi $name'
echo "Hi $name"

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.

shell
#!/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.

shell
echo "script: $0"
echo "first:  $1"
echo "count:  $#"
cp "$1" "$2"       # safe with spaces

9 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.

One email when Bitpath is ready to try. Nothing else. Privacy