Bash 中是否有一个钩子可以找出 cwd 何时发生变化?

发布于 2024-09-10 01:23:57 字数 289 浏览 5 评论 0原文

我通常使用 zsh,它提供了 chpwd() 挂钩。也就是说:如果 cwd 被内置的 cd 改变,zsh 会自动调用 chpwd() 方法(如果存在)。这允许设置依赖于 cwd 的变量和别名。

现在我想将 .zshrc 的这一部分移植到 bash,但发现 bash 无法识别 chpwd()。 bash 中是否已经存在类似的功能?我知道重新定义 cd 是可行的(见下文),但我的目标是寻求更优雅的解决方案。

function cd()
{
    builtin cd $@
    chpwd
}

I am usually using zsh, which provides the chpwd() hook. That is: If the cwd is changed by the cd builtin, zsh automatically calls the method chpwd() if it exists. This allows to set up variables and aliases which depend on the cwd.

Now I want to port this bit of my .zshrc to bash, but found that chpwd() is not recognized by bash. Is a similar functionality already existing in bash? I'm aware that redefining cd works (see below), yet I'm aiming for a more elegant solution.

function cd()
{
    builtin cd $@
    chpwd
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-09-17 01:23:57

您必须使用 DEBUG trapPROMPT_COMMAND

示例:

trap chpwd DEBUG        # calls the function before each command

PROMPT_COMMAND=chpwd    # calls the function after each command

请注意,PROMPT_COMMAND 中定义的函数在每个 提示符之前运行,即使是空提示符也是如此。

You would have to use a DEBUG trap or PROMPT_COMMAND.

Examples:

trap chpwd DEBUG        # calls the function before each command

PROMPT_COMMAND=chpwd    # calls the function after each command

Note that the function defined in PROMPT_COMMAND is run before each prompt, though, even empty ones.

深府石板幽径 2024-09-17 01:23:57

更好的解决方案可能是定义自定义chpwd挂钩

与其他现代 shell 相比,Bash 没有设计完整的钩子系统。 PROMPT_COMMAND变量用作钩子函数,相当于ZSH中的precmd钩子,Fish中的fish_prompt。目前,ZSH 是我所知道的唯一一个内置 chpwd 钩子的 shell。

PROMPT_COMMAND

如果设置,该值将被解释为在打印每个主提示 ($PS1) 之前执行的命令。

https://www.gnu。 org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Variables

chpwd Bash 中的钩子

提供了在 Bash 中设置 chpwd 等效钩子的技巧基于PROMPT_COMMAND

# create a PROPMT_COMMAND equivalent to store chpwd functions
typeset -g CHPWD_COMMAND=""

_chpwd_hook() {
  shopt -s nullglob

  local f

  # run commands in CHPWD_COMMAND variable on dir change
  if [[ "$PREVPWD" != "$PWD" ]]; then
    local IFS=

使用

# example 1: `ls` list directory once dir is changed
_ls_on_cwd_change() {
  ls
}

# append the command into CHPWD_COMMAND
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}_ls_on_cwd_change"

# or just use `ls` directly
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}ls"

来源:根据我的要点在 Bash 中创建 chpwd 等效 Hook

;' for f in $CHPWD_COMMAND; do "$f" done unset IFS fi # refresh last working dir record export PREVPWD="$PWD" } # add `;` after _chpwd_hook if PROMPT_COMMAND is not empty PROMPT_COMMAND="_chpwd_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"

使用

来源:根据我的要点在 Bash 中创建 chpwd 等效 Hook

A better solution could be defining a custom chpwd hook.

There's not a complete hook system designed in Bash when compared with other modern shells. PROMPT_COMMAND variable is used as a hook function, which is equivalent to precmd hook in ZSH, fish_prompt in Fish. For the time being, ZSH is the only shell I've known that has a chpwd hook builtin.

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Variables

chpwd Hook in Bash

A trick is provided to setup a chpwd equivalent hook in Bash based on PROMPT_COMMAND.

# create a PROPMT_COMMAND equivalent to store chpwd functions
typeset -g CHPWD_COMMAND=""

_chpwd_hook() {
  shopt -s nullglob

  local f

  # run commands in CHPWD_COMMAND variable on dir change
  if [[ "$PREVPWD" != "$PWD" ]]; then
    local IFS=

Usage

# example 1: `ls` list directory once dir is changed
_ls_on_cwd_change() {
  ls
}

# append the command into CHPWD_COMMAND
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}_ls_on_cwd_change"

# or just use `ls` directly
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}ls"

Source: Create chpwd Equivalent Hook in Bash from my gist.

;' for f in $CHPWD_COMMAND; do "$f" done unset IFS fi # refresh last working dir record export PREVPWD="$PWD" } # add `;` after _chpwd_hook if PROMPT_COMMAND is not empty PROMPT_COMMAND="_chpwd_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"

Usage

Source: Create chpwd Equivalent Hook in Bash from my gist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文