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

发布于 2024-08-28 13:24:05 字数 313 浏览 3 评论 0原文

如何找出系统上定义别名的位置?我指的是从 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 技术交流群。

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

发布评论

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

评论(12

靖瑶 2024-09-04 13:24:05

对于 OSX,这个两步序列对我来说效果很好,可以找到我很久以前创建的别名,但无法找到预期的位置 (~/.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'

啊哈! “隐藏”在 ~/.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).

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.

倥絔 2024-09-04 13:24:05

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

grep -RHi "alias" /etc /root

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

grep -RHi "alias" /etc /root
尘曦 2024-09-04 13:24:05

首先使用以下命令

列出所有函数

functions 

列出所有别名

alias 

如果找不到别名或函数,请考虑更积极的搜索方法

Bash 版本

bash -ixlc : 2>&1 | grep thingToSearchHere

Zsh 版本

zsh -ixc : 2>&1 | grep thingToSearchHere

选项简要说明

-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

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 -ixlc : 2>&1 | grep thingToSearchHere

Zsh version

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
独夜无伴 2024-09-04 13:24:05

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

  • /etc/profile
  • ~/.bash_profile 或 ~/.bash_login 或 ~/.profile
  • ~/.bash_logout
  • ~/.bashrc

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

Also in future these are the standard bash config files

  • /etc/profile
  • ~/.bash_profile or ~/.bash_login or ~/.profile
  • ~/.bash_logout
  • ~/.bashrc

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

笑饮青盏花 2024-09-04 13:24:05

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

[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

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

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:

[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:

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
赤濁 2024-09-04 13:24:05

除了 .zshrc/.bashrc/.profile 等文件之外,请检查您添加的自定义安装/插件/插件

所以对我来说:它是 git 别名为“g”。

$ which g
g: aliased to git

然后我运行以下命令列出所有别名,

$ alias

我发现了很多与 git 相关的别名,我知道我没有手动添加这些别名。
这让我开始思考我安装的软件包或配置。于是就去了

.oh-my-zsh
目录。在这里我运行了以下命令:

$ grep -r 'git' . |grep -i alias

你瞧,我在以下位置找到了我的别名:

./plugins/git/git.plugin.zsh

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

$ which g
g: aliased to git

Then I ran the following command to list all aliases

$ alias

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

.oh-my-zsh
directory. Here I ran the following command:

$ grep -r 'git' . |grep -i alias

And lo and behold, I found my alias in :

./plugins/git/git.plugin.zsh

心如狂蝶 2024-09-04 13:24:05

我找到了答案(我一直盯着正确的文件但错过了明显的)。

在我的例子中,别名是在文件 ~/.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.

盗梦空间 2024-09-04 13:24:05

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

例如:

echo "before sourcing .bash-it:"
alias mysql
. $HOME/.bash-it/bash-it.sh
echo "after sourcing bash:"
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.:

echo "before sourcing .bash-it:"
alias mysql
. $HOME/.bash-it/bash-it.sh
echo "after sourcing bash:"
alias mysql
五里雾 2024-09-04 13:24:05

我认为这可能类似于 ghostdog74 意味着他们的命令对我不起作用。

我会尝试这样的事情:

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 [[ 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:

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>

想挽留 2024-09-04 13:24:05

查找别名定义位置的唯一可靠方法是使用 dtruss.

如果

$ csrutil status
System Integrity Protection status: enabled.

您无法打开 bash,您可能需要一份副本。

$ cp /bin/bash mybash
$ $ codesign --remove-signature mybash

然后用于

sudo dtruss -t open ./mybash -ic exit 2>&1 | awk -F'"' '/^open/ {print substr($2, 0, length($2)-2)}'

列出可以定义别名的所有文件,例如

/dev/dtracehelper
/dev/tty
/usr/share/locale/en_CA.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA/LC_MESSAGES/BASH.mo
/usr/share/locale/en.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en/LC_MESSAGES/BASH.mo
/Users/user/.bashrc
/Users/user/.bash_aliases
/Users/user/.bash_history
...

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

$ csrutil status
System Integrity Protection status: enabled.

you won't be able to open bash and you may need a copy.

$ cp /bin/bash mybash
$ $ codesign --remove-signature mybash

and then use

sudo dtruss -t open ./mybash -ic exit 2>&1 | awk -F'"' '/^open/ {print substr($2, 0, length($2)-2)}'

to list all the files where the alias could have been defined, like

/dev/dtracehelper
/dev/tty
/usr/share/locale/en_CA.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA/LC_MESSAGES/BASH.mo
/usr/share/locale/en.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en/LC_MESSAGES/BASH.mo
/Users/user/.bashrc
/Users/user/.bash_aliases
/Users/user/.bash_history
...
ペ泪落弦音 2024-09-04 13:24:05

尝试: 别名 | 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

笑饮青盏花 2024-09-04 13:24:05

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

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

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