AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 772220
Accepted
Erwann
Erwann
Asked: 2024-03-13 14:10:06 +0800 CST2024-03-13 14:10:06 +0800 CST 2024-03-13 14:10:06 +0800 CST

获取当前 shell 环境和干净 shell 环境之间“declare -F”的增量

  • 772

主要问题:如何获得declare -F当前 shell 中的值与 shell 刚刚启动时的值之间的增量(下面的前两个命令)。$(declare -F)并不能解决问题,因为子shell是shell进程的副本。子公司:为什么下面第三个命令没有输出任何内容?

$ exec env -i bash                   
$ declare -F      
declare -f ShowInstallerIsoInfo
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __load_completion
declare -f __ltrim_colon_completions
declare -f __parse_options
declare -f __reassemble_comp_words_by_ref
declare -f _allowed_groups
declare -f _allowed_users
declare -f _available_interfaces
declare -f _bashcomp_try_faketty
declare -f _bq_completer
declare -f _cd
declare -f _cd_devices
declare -f _command
declare -f _command_offset
declare -f _complete_as_root
declare -f _completer
declare -f _completion_loader
declare -f _configured_interfaces
declare -f _count_args
declare -f _dvd_devices
declare -f _expand
declare -f _filedir
declare -f _filedir_xspec
declare -f _fstypes
declare -f _get_comp_words_by_ref
declare -f _get_cword
declare -f _get_first_arg
declare -f _get_pword
declare -f _gids
declare -f _have
declare -f _included_ssh_config_files
declare -f _init_completion
declare -f _installed_modules
declare -f _ip_addresses
declare -f _kernel_versions
declare -f _known_hosts
declare -f _known_hosts_real
declare -f _longopt
declare -f _mac_addresses
declare -f _minimal
declare -f _modules
declare -f _ncpus
declare -f _open_files_for_editing
declare -f _parse_help
declare -f _parse_usage
declare -f _pci_ids
declare -f _pgids
declare -f _pids
declare -f _pnames
declare -f _python_argcomplete
declare -f _quote_readline_by_ref
declare -f _realcommand
declare -f _rl_enabled
declare -f _root_command
declare -f _service
declare -f _services
declare -f _shells
declare -f _signals
declare -f _split_longopt
declare -f _sysvdirs
declare -f _terms
declare -f _tilde
declare -f _uids
declare -f _upvar
declare -f _upvars
declare -f _usb_ids
declare -f _user_at_host
declare -f _usergroup
declare -f _userland
declare -f _variables
declare -f _xfunc
declare -f _xinetd_services
declare -f dequote
declare -f quote
declare -f quote_readline
$ "$SHELL" -c 'declare -F'   

其他:

$ uname -a
Linux elitebook 6.7.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 02 Feb 2024 17:03:55 +0000 x86_64 GNU/Linux
$ bash --version
GNU bash, version 5.2.26(1)-release (x86_64-pc-linux-gnu)
$ echo $SHELL
/bin/bash

更新:

干净状态是一个空函数列表,如输出所示bash -c 'declare -F'

根据第一个命令,我预计第二个命令会输出两个函数而不是零。

$ grep -F -f <(declare -F | cut --delimiter=' ' --fields=3) ~/.bashrc | grep -v -E '^#' |  wc -l
2
$ bash -c 'source ~/.bashrc; declare -F'

更新2

我期望第二个输出两个函数而不是零。

采购不生效的原因可能是:

$ cat ~/.bashrc
#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

解决方法确实输出两个函数。

$ bash -c 'source <(grep -v '\''!= *i*'\'' ~/.bashrc); compgen -A function' 2>/dev/null
subshell
  • 1 1 个回答
  • 27 Views

1 个回答

  • Voted
  1. Best Answer
    Stéphane Chazelas
    2024-03-13T15:04:52+08:002024-03-13T15:04:52+08:00

    如果您想知道自 shell 启动并获取启动文件以来函数列表中发生了什么变化,因为在 bash 中,函数名称无论如何都不能包含换行符,您可以使用compgen -A function每行一个转储函数列表,并且comm比较不同时间点的转储(需要在同一区域设置中排序)。

    添加:

    initial_functions=$(compgen -A function | LC_ALL=C sort)
    

    在你的末尾~/.bashrc,然后运行:

    LC_ALL=C comm -13 <(printf '%s\n' "$initial_functions") \
                      <(compgen -A function | LC_ALL=C sort)
    

    查看此后添加了哪些内容。

    LC_ALL=C comm -23 <(printf '%s\n' "$initial_functions") \
                      <(compgen -A function | LC_ALL=C sort)
    

    对于那些已经被删除的。

    zsh 等效项是:

    initial_functions=( ${(k)functions} )
    () { print -rC1 -- ${argv:|initial_functions}; } ${(k)functions}
    () { print -rC1 -- ${initial_functions:|argv}; } ${(k)functions}
    

    无论如何,请注意,像 zsh1 这样的 bash 本身并不定义任何函数,因此干净状态是一个空函数列表,如 的输出所示bash -c 'declare -F'。

    您可以看到的所有函数要么是从环境中导入的(因为bash支持通过BASH_FUNC_funcname%%=环境变量导出函数),要么是在 bash 代码中定义的,该代码自启动以来export -f一直被解释,可能是通过文件或任何bash 启动文件,一些其中可能由您的操作系统提供,或来自那里的文件,例如您似乎正在使用的第三方项目的文件。bash$BASHENVbash-completion


    然而,zsh 提供了大量的函数,您可以将它们作为示例函数加载或自动加载,或者支持它的几个子系统,例如完成(在 zsh 中不是一个单独的项目)、FTP、行编辑器、日历、提示配置、文档,以及一些有用的工具,例如zargs,,zmv...zcalc

    • 1

相关问题

  • bash:使用子shell分配给变量,但如果分配失败则退出主脚本

  • 外壳数学是否在子外壳中运行?

  • 如何在子 shell 退出之前停止子 shell 的子进程(根据 SIGSTOP)?

  • $() 是一个子shell吗?

  • 如何在与 shell 脚本不同的目录中执行 ant 脚本?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve