Funny way with sorting:
if you have an array of integers, you can use sort to sort it, then select the first and last elements for the min and max elements, for example:
{ read min; max=$(tail -n1); } < <(printf "%s\n" "${array[@]}" | sort -n)
So, if you want to invite the user to 10 integers, make sure that the user has entered integers and then sorted them, you could do:
#!/bin/bash n=10 array=() while ((n));do read -p "[$n] Give me an integer: " i [[ $i =~ ^[+-]?[[:digit:]]+$ ]] || continue array+=($i) ((--n)) done # Sort the array: { read min; max=$(tail -n1); } < <(printf "%s\n" "${array[@]}" | sort -n) # print min and max elements: echo "min=$min" echo "max=$max"
source share