在 Mac OS X 中运行终端时如何找出别名(在 bash 意义上)的定义位置
如何找出系统上定义别名的位置?我指的是从 Mac OS X (10.6.3) 启动的终端会话中使用的别名。
例如,如果我在终端命令提示符下输入不带任何参数的 alias
命令,我会得到我已设置的别名列表,例如:
alias mysql='/usr/local/mysql/bin/mysql'
但是,我已使用 Spotlight 在我的系统中进行了搜索和 mdfind
在各种启动文件中,到目前为止找不到定义此别名的位置。 (我很久以前就做过了,没有写下我在哪里分配了别名)。
How can I find out where an alias is defined on my system? I am referring to the kind of alias that is used within a Terminal session launched from Mac OS X (10.6.3).
For example, if I enter the alias
command with no parameters at a Terminal command prompt, I get a list of aliases that I have set, for example:
alias mysql='/usr/local/mysql/bin/mysql'
However, I have searched all over my system using Spotlight and mdfind
in various startup files and so far can not find where this alias has been defined. ( I did it a long time ago and didn't write down where I assigned the alias).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于 OSX,这个两步序列对我来说效果很好,可以找到我很久以前创建的别名,但无法找到预期的位置 (~/.zshrc)。
啊哈! “隐藏”在
~/.oh-my-zsh/lib/aliases.zsh
中。我在 .oh-my-zsh 中浏览了一下,但忽略了 lib/aliases.zsh。For OSX, this 2-step sequence worked well for me, in locating an alias I'd created long ago and couldn't locate in expected place (~/.zshrc).
Aha! "Hiding" in
~/.oh-my-zsh/lib/aliases.zsh
. I had poked around a bit in .oh-my-zsh but had overlooked lib/aliases.zsh.您只需在命令提示符下输入
alias
即可查看您拥有的别名。否则,您可以在定义别名的最常见位置进行查找
,例如you can just simply type in
alias
on the command prompt to see what aliases you have. Otherwise, you can do afind
on the most common places where aliases are defined, eg首先使用以下命令
列出所有函数
列出所有别名
如果找不到别名或函数,请考虑更积极的搜索方法
Bash 版本
Zsh 版本
选项简要说明
First use the following commands
List all functions
List all aliases
If you aren't finding the alias or function consider a more aggressive searching method
Bash version
Zsh version
Brief Explanation of Options
另外,将来这些是标准的 bash 配置文件
更多信息:http://www.heimhardt.com/htdocs/bashrcs.html
Also in future these are the standard bash config files
More info: http://www.heimhardt.com/htdocs/bashrcs.html
聚会有点晚了,但我遇到了同样的问题(试图找到“l.”命令在 RHEL6 中的别名),并最终到达了之前答案中未提及的地方。可能无法在所有 bash 实现中找到它,但如果 /etc/profile.d/ 目录存在,请尝试在那里 grep 查找无法解释的别名。这就是我发现的地方:
bash 手册页中没有提到该目录,并且不是 bash 搜索配置文件/启动信息的正确部分,但对于 RHEL,您可以在 /etc/profile 中看到调用代码:
A bit late to the party, but I was having the same problem (trying to find where the "l." command was aliased in RHEL6), and ended up in a place not mentioned in the previous answers. It may not be found in all bash implementations, but if the /etc/profile.d/ directory exists, try grepping there for unexplained aliases. That's where I found:
The directory isn't mentioned in the bash manpage, and isn't properly part of where bash searches for profile/startup info, but in the case of RHEL you can see the calling code within /etc/profile:
除了 .zshrc/.bashrc/.profile 等文件之外,请检查您添加的自定义安装/插件/插件
所以对我来说:它是 git 别名为“g”。
然后我运行以下命令列出所有别名,
我发现了很多与 git 相关的别名,我知道我没有手动添加这些别名。
这让我开始思考我安装的软件包或配置。于是就去了
你瞧,我在以下位置找到了我的别名:
Please do check custom installations/addons/plugins you have added, in addition to the .zshrc/.bashrc/.profile etc files
So for me: it was git aliased to 'g'.
Then I ran the following command to list all aliases
I found a whole lot of git related aliases that I knew I had not manually added.
This got me thinking about packages or configurations I had installed. And so went to the
And lo and behold, I found my alias in :
我找到了答案(我一直盯着正确的文件但错过了明显的)。
在我的例子中,别名是在文件 ~/.bash_profile 中定义的,
不知何故,这让我困惑。
I found the answer ( I had been staring at the correct file but missed the obvious ).
The aliases in my case are defined in the file ~/.bash_profile
Somehow this eluded me.
对于更复杂的设置(例如,当您使用 bash-it、oh-my-zsh 等 shell 脚本框架时),在脚本中的关键位置添加“alias mysql”通常很有用。这将帮助您准确地确定添加别名的时间。
例如:
For more complex setups (e.g. when you're using a shell script framework like bash-it, oh-my-zsh or the likes) it's often useful to add 'alias mysql' at key positions in your scripts. This will help you figure out exactly when the alias is added.
e.g.:
我认为这可能类似于 ghostdog74 意味着他们的命令对我不起作用。
我会尝试这样的事情:
如果你想真正花哨,你甚至可以添加一个
if [[ grep -c "alias" ]] then
I think that maybe this is similar to what ghostdog74 meant however their command didn't work for me.
I would try something like this:
If you wanted to be really fancy you could even add an
if [[ grep -c "alias" ]] then <print file name>
查找别名定义位置的唯一可靠方法是使用 dtruss.
如果
您无法打开
bash
,您可能需要一份副本。然后用于
列出可以定义别名的所有文件,例如
The only reliable way of finding where the alias could have been defined is by analyzing the list of files opened by
bash
using dtruss.If
you won't be able to open
bash
and you may need a copy.and then use
to list all the files where the
alias
could have been defined, like尝试:
别名 | grep name_of_alias
例如:
别名 | grep mysql
或者,如上所述
which name_of_alias
Try:
alias | grep name_of_alias
Ex.:
alias | grep mysql
or, as already mentioned above
which name_of_alias
就我而言,我使用 Oh My Zsh,因此我将别名定义放在 ~/.zshrc 文件中。
In my case, I use Oh My Zsh, so I put aliases definition in ~/.zshrc file.