#!/bin/bash declare -A orbits declare -A cost while IFS=\) read -r a b; do orbits[$b]=$a done sum=0 o=${orbits[YOU]} while [[ $o != COM ]]; do cost[$o]=$((sum++)) o=${orbits[$o]} done cost[COM]=$sum sum2=0 o=${orbits[SAN]} while [[ $o != COM ]]; do if [[ ${cost[$o]} ]]; then echo "$((cost[$o] + sum2))" exit fi ((sum2++)) o=${orbits[$o]} done