如何筛选 functions/aliases 以仅查找用户定义的?

How can I filter functions/aliases to find only user-defined ones?

我正在尝试查看有关函数和别名的信息,并使用此函数来显示它们的定义。但是,它不仅仅显示我的配置文件脚本定义的函数/别名,还显示了很多其他的东西(我不太感兴趣)。

def ()
{
    if [ -z "" ]; then
        declare -F;
        printf "\nAbove listing is all defined functions 'declare -F' (use def <func-name> to show function contents)\nType 'alias' to show all aliases (def <alias-nam> to show alias definition, where 'def' uses 'command -V <name>')\n\n";
    else
        command -V ;
    fi
}

一种方法,将已知函数列表缓存在点文件的顶部(因此任何未缓存的都可以视为未知):

declare -A -g predefined_commands

populate_predefined_commands() {
  local line fndec_re alias_re
  fndec_re='^declare -f ([^[:space:]]+)$'
  alias_re='^alias ([^[:space:]=])+='
  while IFS= read -r line; do
    [[ $line =~ $fndec_re ]] && predefined_commands[${BASH_REMATCH[1]}]=1
  done < <(declare -F)
  while IFS= read -r line; do
    [[ $line =~ $alias_re ]] && predefined_commands[${BASH_REMATCH[1]}]=1
  done < <(alias -p)
}

def() {
  if [[  ]]; then
    command -V -- ""
    return
  fi

  local line fndec_re alias_re
  fndec_re='^declare -f ([^[:space:]]+)$'
  alias_re='^alias ([^[:space:]=])+='
  while IFS= read -r line; do
    [[ $line =~ $fndec_re ]] && [[ ${predefined_commands[${BASH_REMATCH[1]}]} ]] && continue
    printf '%s\n' "$line"
  done < <(declare -F)
  while IFS= read -r line; do
    [[ $line =~ $alias_re ]] && [[ ${predefined_commands[${BASH_REMATCH[1]}]} ]] && continue
    printf '%s\n' "$line"
  done < <(alias -p)
}

# do this AFTER calling def, or else def itself will show up as a new function
populate_predefined_commands; unset -f populate_predefined_commands