获取“别名”中的“pwd”?

发布于 2024-10-31 01:35:48 字数 468 浏览 5 评论 0原文

有没有办法可以在我的 .zshrc 文件中的 alias 中获取 pwd ?我正在尝试做类似以下的事情:

alias cleanup="rm -Rf `pwd`/{foo,bar,baz}"

这在 bash 中工作得很好; pwd 始终是我 cd 进入的目录,但是在 zsh 中,似乎是在首次加载 .zshrc 文件时对其进行评估并始终保留为我的主目录。我已经使用非常简单的 alias 设置进行了测试,但它永远不会改变。

我怎样才能进行此更改,以便从子目录调用alias始终计算为该子目录?

编辑:不确定这是否有帮助,但我在 Mac 上通过 oh-my-zsh 使用 zsh。

Is there a way I can get the pwd in an alias in my .zshrc file? I'm trying to do something like the following:

alias cleanup="rm -Rf `pwd`/{foo,bar,baz}"

This worked fine in bash; pwd is always the directory I've cd'd into, however in zsh it seems that it's evaluated when the .zshrc file is first loaded and always stays as my home directory. I've tested using with a really simple alias setup, but it never changes.

How can I have this change, so that calling the alias from a subdirectory always evaluates as that subdir?

EDIT: not sure if this will help, but I'm using zsh via oh-my-zsh on the mac.

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

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

发布评论

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

评论(1

白首有我共你 2024-11-07 01:35:48

加载 .zshrc 时,将评估 alias 命令。该命令由两个单词组成:一个命令名称(内置别名)和一个参数,它是扩展 cleanup="rm -Rf `pwd`/{foo,bar ,baz}”。由于反引号插入在双引号之间,因此该参数扩展为 cleanup=rm -Rf /home/unpluggd/{foo,bar,baz} (这是一个 shell 单词),其中 /home/ unpluggd 是当时的当前目录。

如果要避免在定义命令时进行插值,请改用单引号。这几乎总是您想要的别名。

alias cleanup='rm -Rf `pwd`/{foo,bar,baz}'

然而,这不必要地复杂。文件名前面不需要 `pwd/`!只需编写

alias cleanup='rm -Rf -- {foo,bar,baz}'

(如果 foo 可能以 - 开头,则需要 --,以避免其被解析为 rm 的选项),由于不再需要大括号,因此可以简化:

alias cleanup='rm -Rf -- foo bar baz'

When your .zshrc is loaded, the alias command is evaluated. The command consists of two words: a command name (the builtin alias), and one argument, which is the result of expanding cleanup="rm -Rf `pwd`/{foo,bar,baz}". Since backquotes are interpolated between double quotes, this argument expands to cleanup=rm -Rf /home/unpluggd/{foo,bar,baz} (that's a single shell word) where /home/unpluggd is the current directory at that time.

If you want to avoid interpolation at the time the command is defined, use single quotes instead. This is almost always what you want for aliases.

alias cleanup='rm -Rf `pwd`/{foo,bar,baz}'

However this is needlessly complicated. You don't need `pwd/` in front of file names! Just write

alias cleanup='rm -Rf -- {foo,bar,baz}'

(the -- is needed if foo might begin with a -, to avoid its being parsed as an option to rm), which can be simplified since the braces are no longer needed:

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