Input, Quotes and Values

Give a script something to work on, ask the user questions, and build values from other commands.

Updated

Input

  1. Arguments: $1, $# and "$@" (Apprentice): The words typed after a script's name are its arguments: $1 is the first, $2 the second, $# says how many there are, and "$@" hands all of them on, each in one piece.
  2. Asking Questions: read (Apprentice): read waits for the user to type a line and keeps it in a variable: -p shows a question first, -s hides what's typed, and ${name:-default} gives an answer that was left empty a default.

Values

  1. Quotes: Double, Single and None (Journeyman): Without quotes, the shell splits a variable's value at its spaces and turns a * into file names, the number-one bash bug; "$name" in double quotes keeps the value exactly as it is, and single quotes keep everything as typed.
  2. Values from Commands: $( ) and $(( )) (Apprentice): $( ) runs a command and puts what it prints into another command or a variable, such as today's date in a backup's name, and $(( )) works out whole-number arithmetic, such as an age or a count plus one.

Exercise: The Greeting Card Maker

  1. Exercise: The Greeting Card Maker (Apprentice): Finish card.sh, a small application that takes a name as its argument, asks for a greeting and a signature, and saves a dated, numbered card, then fix its quoting bug and let a check script run it for real.