进入目录后执行 bash 函数

发布于 2024-09-11 13:20:37 字数 245 浏览 13 评论 0原文

我想在进入新目录时执行特定的 bash 函数。有些人认为:

alias cd="cd $@ && myfunction"

$@ 在那里不起作用,并且添加反斜杠也没有帮助。我也有点担心弄乱 cd,如果这适用于其他更改目录的命令,例如 pushdpopd ,那就太好了。

有更好的别名/命令吗?

I'd like to execute a particular bash function when I enter a new directory. Somethink like:

alias cd="cd $@ && myfunction"

$@ doesn't work there, and adding a backslash doesn't help. I'm also a little worried about messing with cd, and it would be nice if this worked for other commands which changed directory, like pushd and popd.

Any better aliases/commands?

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

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

发布评论

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

评论(4

青朷 2024-09-18 13:20:37

别名不接受参数。你应该使用一个函数。无需在每次发出提示时自动执行它。

function cd () { builtin cd "$@" && myfunction; }

builtin 关键字允许您重新定义 Bash 内置函数,而无需创建递归。引用该参数可以使其在目录名称中有空格的情况下起作用。

Bash 文档 说:

对于几乎所有用途,shell 函数都优于别名。

Aliases don't accept parameters. You should use a function. There's no need to execute it automatically every time a prompt is issued.

function cd () { builtin cd "$@" && myfunction; }

The builtin keyword allows you to redefine a Bash builtin without creating a recursion. Quoting the parameter makes it work in case there are spaces in directory names.

The Bash docs say:

For almost every purpose, shell functions are preferred over aliases.

吻泪 2024-09-18 13:20:37

我能想到的最简单的解决方案就是这个

myfunction() {
  if [ "$PWD" != "$MYOLDPWD" ]; then
    MYOLDPWD="$PWD";
    # strut yer stuff here..
  fi
}

export PROMPT_COMMAND=myfunction

,应该可以做到。它将适用于所有命令,并将在显示提示之前触发。

The easiest solution I can come up with is this

myfunction() {
  if [ "$PWD" != "$MYOLDPWD" ]; then
    MYOLDPWD="$PWD";
    # strut yer stuff here..
  fi
}

export PROMPT_COMMAND=myfunction

That ought to do it. It'll work with all commands, and will get triggered before the prompt is displayed.

你的背包 2024-09-18 13:20:37

还有一些其他版本,包括

  • 我编写的 smartcd,它具有大量功能,包括模板和临时变量保存
  • ondir,更小,更简单

这两个都支持 bash 和 zsh

There are a few other versions of this out there, including

  • smartcd, which I wrote, and has a ton of features including templating and temporary variable saving
  • ondir, which is smaller and much simpler

Both of these support both bash and zsh

野の 2024-09-18 13:20:37

我编写了一个 ZSH 脚本,利用回调函数 chpwd 来获取项目特定的 ZSH 配置。我不确定它是否适用于 Bash,但我认为值得一试。如果它在您要进入的目录中找不到脚本文件,它将检查父目录,直到找到要获取的脚本(或直到到达 /)。当 cd'ing 出目录时,它还会调用一个函数 unmagic,这允许您在离开项目时清理环境。

http://github.com/jkramer/home/blob/master /.zsh/func/magic

“magic”脚本示例:

export BASE=$PWD # needed for another script of mine that allows you to cd into the projects base directory by pressing ^b

ctags -R --languages=Perl $PWD # update ctags file when entering the project directory

export PERL5LIB="$BASE/lib"

# function that starts the catalyst server
function srv {
  perl $BASE/script/${PROJECT_NAME}_server.pl
}

# clean up
function unmagic {
  unfunction src
  unset PERL5LIB
}

I've written a ZSH script utilizing the callback function chpwd to source project specific ZSH configurations. I'm not sure if it works with Bash, but I think it'll be worth a try. If it doesn't find a script file in the directory you're cd'ing into, it'll check the parent directories until it finds a script to source (or until it reaches /). It also calls a function unmagic when cd'ing out of the directory, which allows you to clean up your environment when leaving a project.

http://github.com/jkramer/home/blob/master/.zsh/func/magic

Example for a "magic" script:

export BASE=$PWD # needed for another script of mine that allows you to cd into the projects base directory by pressing ^b

ctags -R --languages=Perl $PWD # update ctags file when entering the project directory

export PERL5LIB="$BASE/lib"

# function that starts the catalyst server
function srv {
  perl $BASE/script/${PROJECT_NAME}_server.pl
}

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