A function gives a few lines a name. Define it once, then run it by name, like a command. Inside it, $1, $2 … are the function's arguments:
opens() {
echo "$1 opens at $2"
}
opens north 07:00
opens "east side" 08:00north opens at 07:00
east side opens at 08:00 ← "east side" is one argument: $1Define a function above the first line that calls it: bash reads from the top, so an earlier call gets opens: command not found.
~/daily.sh pastes the same three banner lines above each shop's note. Make the banner one place to change:
- Run
./daily.shto see what it prints. - Open it with
nano ~/daily.sh. Under the comments, definebannerwith the three banner lines. Replace each pasted copy with one line:banner. - Below
banner, definenote, which prints the banner and the note of the shop you give it. Replace eachbannerandgreppair with one line:note north,note south,note "east side". - Run
./daily.shagain: the same output as before.
Objectives
- Define a function
bannerthat prints the three banner lines. ./daily.shprints the same as before, with the banner's lines in the file only once.- Define
note, which prints the banner and one shop's note. Thegrepline is in the file only once.