在“别名"中获取“密码"? [英] Get the `pwd` in an `alias`?

查看:32
本文介绍了在“别名"中获取“密码"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在我的 .zshrc 文件中的 alias 中获取 pwd ?我正在尝试执行以下操作:

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}"

这在 bash 中运行良好;pwd 始终是我 cd 进入的目录,但是在 zsh 中似乎在第一次加载 .zshrc 文件时对其进行评估并始终作为我的主目录.我已经使用非常简单的 alias 设置进行了测试,但它从未改变.

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.

如何进行此更改,以便从子目录调用 alias 始终计算为该子目录?

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

不确定这是否会有所帮助,但我在 mac 上通过 oh-my-zsh 使用 zsh.

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

推荐答案

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

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}'

然而,这不必要地复杂化.文件名前不需要`pwd/`!随便写

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

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

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

(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'

这篇关于在“别名"中获取“密码"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆