在 Mac OS X 中运行终端时如何找出别名(在 bash 意义上)的定义位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2614403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:09:49  来源:igfitidea点击:

How to find out where alias (in the bash sense) is defined when running Terminal in Mac OS X

terminalaliasbash

提问by Richard Fuhr

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).

如何找出系统上定义别名的位置?我指的是在从 Mac OS X (10.6.3) 启动的终端会话中使用的那种别名。

For example, if I enter the aliascommand with no parameters at a Terminal command prompt, I get a list of aliases that I have set, for example:

例如,如果我alias在终端命令提示符下输入不带参数的命令,我会得到我设置的别名列表,例如:

alias mysql='/usr/local/mysql/bin/mysql'

However, I have searched all over my system using Spotlight and mdfindin 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).

但是,我已经使用 Spotlight 在我的整个系统和mdfind各种启动文件中进行了搜索,但到目前为止找不到定义此别名的位置。(我很久以前就做了,没有写下我分配别名的位置)。

回答by cweekly

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).

对于 OSX,这个 2 步序列对我来说效果很好,在定位我很久以前创建的别名时无法定位到预期的位置 (~/.zshrc)。

cweekly:~ $ which la
la: aliased to ls -lAh

cweekly:~$ grep -r ' ls -lAh' ~
/Users/cweekly//.oh-my-zsh/lib/aliases.zsh:alias la='ls -lAh'

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.

啊哈!“隐藏”在~/.oh-my-zsh/lib/aliases.zsh。我在 .oh-my-zsh 中略过一些,但忽略了 lib/aliases.zsh。

回答by ghostdog74

you can just simply type in aliason the command prompt to see what aliases you have. Otherwise, you can do a findon the most common places where aliases are defined, eg

您只需在alias命令提示符下输入即可查看您拥有的别名。否则,您可以find在定义别名的最常见位置执行 a ,例如

grep -RHi "alias" /etc /root

回答by sixtyfootersdude

Also in future these are the standard bash config files

将来这些是标准的 bash 配置文件

  • /etc/profile
  • ~/.bash_profile or ~/.bash_login or ~/.profile
  • ~/.bash_logout
  • ~/.bashrc
  • /etc/配置文件
  • ~/.bash_profile 或 ~/.bash_login 或 ~/.profile
  • ~/.bash_logout
  • ~/.bashrc

More info: http://www.heimhardt.com/htdocs/bashrcs.html

更多信息:http: //www.heimhardt.com/htdocs/bashrcs.html

回答by jasonleonhard

First use the following commands

首先使用以下命令

List all functions

列出所有函数

functions 

List all aliases

列出所有别名

alias 

If you aren't finding the alias or function consider a more aggressive searching method

如果您没有找到别名或函数,请考虑使用更积极的搜索方法

Bash version

bash 版本

bash -ixlc : 2>&1 | grep thingToSearchHere

Zsh version

zsh版本

zsh -ixc : 2>&1 | grep thingToSearchHere

Brief Explanation of Options

选项的简要说明

-i     Force shell to be interactive.

-c     Take the first argument as a command to execute

-x      -- equivalent to --xtrace

-l      Make bash act as if invoked as a login shell

回答by Stovey

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:

聚会有点晚了,但我遇到了同样的问题(试图找到“l.”命令在 RHEL6 中的别名),并最终出现在之前的答案中没有提到的地方。它可能不会在所有 bash 实现中找到,但如果 /etc/profile.d/ 目录存在,请尝试在那里搜索无法解释的别名。那就是我发现的地方:

[user@server ~]$ grep l\. /etc/profile.d/*
/etc/profile.d/colorls.csh:alias l. 'ls -d .*'
/etc/profile.d/colorls.csh:alias l. 'ls -d .* --color=auto'
/etc/profile.d/colorls.sh:  alias l.='ls -d .*' 2>/dev/null
/etc/profile.d/colorls.sh:alias l.='ls -d .* --color=auto' 2>/dev/null

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:

该目录未在 bash 手册页中提及,也不是 bash 搜索配置文件/启动信息的正确部分,但在 RHEL 的情况下,您可以在 /etc/profile 中看到调用代码:

for i in /etc/profile.d/*.sh ; do
  if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then
      . "$i"
    else
      . "$i" >/dev/null 2>&1
    fi
  fi
done

回答by user1372408

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.

对于更复杂的设置(例如,当您使用 bash-it、oh-my-zsh 等 shell 脚本框架时),在脚本的关键位置添加“别名 mysql”通常很有用。这将帮助您准确确定添加别名的时间。

e.g.:

例如:

echo "before sourcing .bash-it:"
alias mysql
. $HOME/.bash-it/bash-it.sh
echo "after sourcing bash:"
alias mysql

回答by Richard Fuhr

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

我的情况下的别名在文件 ~/.bash_profile 中定义

Somehow this eluded me.

不知何故,这让我望而却步。

回答by sixtyfootersdude

I think that maybe this is similar to what ghostdog74meant however their command didn't work for me.

我认为这可能与ghostdog74 的意思相似,但是他们的命令对我不起作用。

I would try something like this:

我会尝试这样的事情:

for i in `find . -type f`; do   # find all files in/under current dir
echo "========" 
echo $i                         # print file name
cat $i | grep "alias"           # find if it has alias and if it does print the line containing it
done

If you wanted to be really fancy you could even add an if [[ grep -c "alias" ]] then <print file name>

如果你想变得很花哨,你甚至可以添加一个 if [[ grep -c "alias" ]] then <print file name>

回答by Blckmamba

Try: alias | grep name_of_aliasEx.: alias | grep mysql

尝试:alias | grep name_of_alias例如:alias | grep mysql

or, as already mentioned above

或者,如上所述

which name_of_alias

which name_of_alias

回答by ryan

In my case, I use Oh My Zsh, so I put aliases definition in ~/.zshrcfile.

就我而言,我使用 Oh My Zsh,所以我将别名定义放在~/.zshrc文件中。