# User-defined function for finding max of 4 numbers

I have a basic script to find the max of 2 numbers using a user-defined function; but, I need to convert it to accept 4 numbers, and I am having a hard time. Here is the script.

``````#!/bin/bash
echo \$1 \$2 | awk '
{
print max(\$1, \$2)
}

function max(a, b) {
return a > b ? a: b
}'
``````

You would simply execute it by doing: `./scriptname 1 2` (or whatever two numbers you want) and the output would be the max of the two numbers. I think I can just do the following.

``````#!/bin/bash
echo \$1 \$2 \$3 \$4 | awk '
{
print max(\$1, \$2, \$3, \$4)
}

function max(a, b, c, d) {
return a < b ? a: b
}'
``````

I am having trouble with line 7, the "return" line. Any suggestions?
Thanks -CableGuy

• And? Edit it, and look at the preview before submitting. Commented Mar 19, 2018 at 17:37
• Just a heads-up; your title says "finding max of 4 numbers", your initial function says & computes the max, but then your final script/function both say & compute the minimum. Commented Mar 19, 2018 at 18:13
• Yeah i see that, i just forgot to modify it from the original minimum function Commented Mar 19, 2018 at 18:17
• I fixed the min to max Commented Mar 19, 2018 at 20:17
• If we have `max2(a,b)` returning the max of inputs, and want `max4(a,b,c,d)` returning max of inputs. Then not that `max4(a,b,c,d)` = `max2(max2(a,b),max2(c,d))` or equivalently `max2(max2(max2(a,b),c),d)` Commented Mar 19, 2018 at 20:20

If you don't mind using Bash instead of AWK:

``````find_max() {
local max number

max="\$1"

for number in "\${@:2}"; do
if ((number > max)); then
max="\$number"
fi
done

printf '%d\n' "\$max"
}
``````

Usage:

``````\$ find_max 12 64 2 455 23 99
455
``````
• I have to use AWK unfortunately Commented Mar 19, 2018 at 17:48
• @cableguy5533 I see. I've added an `awk` tag but I recommend you to state this in the question itself. I'd be glad to provide an awk-only answer if I knew it. Good luck and welcome to Unix & Linux. Commented Mar 19, 2018 at 18:01

You can use the 2-argument function - multiple times:

``````\$ cat scriptname
#!/bin/bash
echo \$1 \$2 \$3 \$4 | awk '

function min(a, b) {
return a < b ? a: b
}

{
print min(min(min(\$1,\$2),\$3),\$4)
}'
``````

then for example

``````\$ ./scriptname 3 1.2 -0.4 77
-0.4
``````

If you're required to write it as a 4-argument function, then I'd suggest something like

``````function min(a, b) {
return a < b ? a : b
}

function min4(a,b,c,d) {
return min(min(min(a,b),c),d)
}

{
print min4(\$1,\$2,\$3,\$4)
}
``````

You can try with this awk :

``````echo -3.89 -1.2 -0.217 -48 | awk '{a=\$1;for(i=2;i<=NF;i++)a=\$i>a?\$i:a;print a}'
``````

The num-utils package includes `numbound` which solves the general problem, (it's not limited to 4 args):

``````printf "%s\n" 5 88.3 22 -22 -333 5 | numbound
``````

Output:

``````88.3
``````

That can be made into a simple function that parses command line arguments:

``````max() { printf "%s\n" "\$@" | numbound ; }
``````

Example:

``````max 55 33 2 99 22
``````

Output:

``````99
``````