How can I automatically install the latest kernel?

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (Y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
    return
  fi

  # Get the latest kernel
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  # Install the latest kernel
  if [[ -n $latest_kernel ]]; then
    echo "Installing the latest kernel: $latest_kernel"
    sudo mhwd-kernel -i $latest_kernel
  else
    echo "Error: Could not find the latest kernel."
  fi
}