ch() {
  # short-circuit
  if [ "$#" -eq 0 ]; then
    cd || exit 1
    return
  fi

  arg="$1"
  for d in $(printf '%s' "${arg}" | sed -Ee 's/[^/]+/&*/g'); do
    test -d "${d}" || continue
    dirs="${dirs} $(q "${d}")"
  done

  eval "set -- ${dirs}"

  # no match
  if [ "$#" -eq 0 ]; then
    printf 1>&2 '%s: %s: no match\n' "$0" "${arg}"
    return 1
  fi

  # single match
  if [ "$#" -eq 1 ]; then
    cd "$1" || exit 1
    return
  fi

  # multiple matches
  i=0
  for d; do
    i=$((i + 1))
    printf '%d) %s\n' "${i}" "${d}"
  done
  printf '%s\n? ' '-----'
  read -r num

  # do nothing if no number
  if [ "${num}" = "" ]; then
    return 0
  fi

  # invalid number
  if [ "${num}" -lt 1 ] || [ "${num}" -gt "$#" ]; then
    printf 1>&2 '%s: %s: bad number\n' "$0" "${num}"
    return 1
  fi
  # shellcheck disable=SC2250
  eval "cd \"\${$num}\""
}
