Bash, lesson 6
A Bash function is a named group of commands. Once defined, you call it exactly like any other command, by writing its name followed by arguments.
The definition must come before the first call, because Bash runs the script from top to
bottom. Some people write function greet { ... }; the result is the same.
A function does not list parameter names. Instead, the arguments arrive as numbered variables:
| Variable | Meaning |
|---|---|
$1, $2, … | the first, second, … argument |
$# | how many arguments were passed |
"$@" | all arguments, each kept as a separate word |
Copy them into named local variables at the top of the function; the rest of the body then reads like normal code:
2 exercises to try what you just read.
Bash functions do not "return" values like functions in other languages. There are two channels, and you will use both:
Output. The function prints its result, and the caller captures it with command
substitution, $( ):
Exit status. return n ends the function with status n, a number from 0 to 255 where
0 means success. That makes a function usable directly in an if:
Without an explicit return, the status is that of the last command the function ran.
Do not use return to send back numbers like totals: anything above 255 wraps around. Print the
value instead.
By default, every variable in Bash is global: a function that sets count=5 changes the
count everywhere in the script. That causes nasty bugs in larger scripts. Declare function
variables with local so they exist only during the call:
A good rule: every variable a function creates should be local, unless changing a global is
the whole point of the function.
A function shares the script's standard input. A read inside a function reads the next line of
input, just as it would outside. The usual structure is to define helper functions at the top,
then write a main loop that reads input and calls them.
Write a function perimeter that takes a width and a height and prints the perimeter
2 * (width + height). Each input line holds a width and a height. For each line, print
W x H: perimeter P, using $(perimeter ...) to get P.
Sample input:
Sample output:
Write a function is_strong that takes one password and succeeds (exit status 0) when the
password is at least 8 characters long and contains at least one digit; otherwise it
fails. ${#var} gives the length of a variable's value, and [[ $pw == *[0-9]* ]] checks for a
digit.
For each input line, print strong or weak by calling is_strong in an if.
Sample input:
Sample output:
greet() {
echo "Hello, $1!"
}
greet "Ada" # Hello, Ada!
greet "Grace" # Hello, Grace!area() {
local width=$1
local height=$2
echo $(( width * height ))
}size=$(area 3 4)
echo "Area: $size" # Area: 12is_even() {
(( $1 % 2 == 0 )) # the status of this test becomes the function's status
}
if is_even 10; then echo "even"; ficount=100
reset() {
local count=0
echo "inside: $count"
}
reset # inside: 0
echo "$count" # 1003 4
10 13 x 4: perimeter 14
10 x 1: perimeter 22sunshine
sunshine7
abc1weak
strong
weak