Shell Functions
Like other programming languages, the shell may have functions. A function is a subroutine that implements a set of commands and operations.
Like other programming languages, the shell may have functions. A function is a subroutine that implements a set of commands and operations. It is useful for repeated tasks.
# basic construct
function_name {
command...
}Functions are called simply by writing their names. A function call is equivalent to a command. Parameters may be passed to a function, by specifying them after the function name. The first parameter is referred to in the function as $1, the second as $2 etc.
function function_B {
echo "Function B."
}
function function_A {
echo "$1"
}
function adder {
echo "$(($1 + $2))"
}
# FUNCTION CALLS
# Pass parameter to function A
function_A "Function A." # Function A.
function_B # Function B.
# Pass two parameters to function adder
adder 12 56 # 68Try it
Expected output
3 + 5 = 8
5 - 1 = 4
4 * 6 = 24Solution
#!/bin/bash
# enter your function code here
function ENGLISH_CALC {
a=$1
b=$3
signn=$2
if [ $signn == "plus" ]; then
echo "$a + $b = $(($a+$b))"
elif [ $signn == "minus" ]; then
echo "$a - $b = $(($a-$b))"
elif [ $signn == "times" ]; then
echo "$a _ $b = $(($a_$b))"
fi
}
# testing code
ENGLISH_CALC 3 plus 5
ENGLISH_CALC 5 minus 1
ENGLISH_CALC 4 times 6Get the Bash agent pack
A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for Bash. One email, then occasional updates when the tooling shifts. No course pitch.
AGENTS.md now — no email needed.