01 / 08
When it gets long, divide it by name
The script you wrote last lesson has been getting longer.
set -eutrap ... EXIT[ $# -eq 0 ] && { echo ...; exit 2; }grep -c ... > ...COUNT=$(...)if [ ... ]; then ... fiEven at seven lines it takes a moment to read "which part does what". At fifty, more so.
So you give a lump of procedure a name.
greet() { echo "hello $1"}greet tarogreet hanakohello tarohello hanakoThis is a function.
It is worth two things.
1. the same procedure, called again and again2. it has a name, so it reads plainlyThe second is bigger than you might think.
print_usage you know at a glancegrep -c ... | awk ... you have to read itThe name is the explanation. It carries without a comment.