获取“别名”中的“pwd”?
有没有办法可以在我的 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加载
.zshrc
时,将评估alias
命令。该命令由两个单词组成:一个命令名称(内置别名
)和一个参数,它是扩展cleanup="rm -Rf `pwd`/{foo,bar ,baz}”
。由于反引号插入在双引号之间,因此该参数扩展为cleanup=rm -Rf /home/unpluggd/{foo,bar,baz}
(这是一个 shell 单词),其中/home/ unpluggd
是当时的当前目录。如果要避免在定义命令时进行插值,请改用单引号。这几乎总是您想要的别名。
然而,这不必要地复杂。文件名前面不需要
`pwd/`
!只需编写(如果
foo
可能以-
开头,则需要--
,以避免其被解析为rm 的选项
),由于不再需要大括号,因此可以简化:When your
.zshrc
is loaded, thealias
command is evaluated. The command consists of two words: a command name (the builtinalias
), and one argument, which is the result of expandingcleanup="rm -Rf `pwd`/{foo,bar,baz}"
. Since backquotes are interpolated between double quotes, this argument expands tocleanup=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.
However this is needlessly complicated. You don't need
`pwd/`
in front of file names! Just write(the
--
is needed iffoo
might begin with a-
, to avoid its being parsed as an option torm
), which can be simplified since the braces are no longer needed: