SoFunction
Updated on 2025-03-10

Array operation summary and bubble sorting implementation script sharing in shell


#!/bin/sh
#sorting following array
echo "please input a number list:"
read -a arr
for (( i=0 ; i<${#arr[@]} ; i++ ))
do
  for (( j=${#arr[@]} - 1 ; j>i ; j-- ))
  do
    #echo $j
    if  [[ ${arr[j]} -lt ${arr[j-1]} ]]
    then
       t=${arr[j]}
       arr[j]=${arr[j-1]}
       arr[j-1]=$t
    fi
  done
done
echo "after sorting:"
echo ${arr[@]}
[tech@ebs sqlee]$ ./
please inout a number list:
0 2 9 6 8 5 7 4 3 1
after sorting:
0 1 2 3 4 5 6 7 8 9