zsh 和 bash 中 which 命令的不同行为 [英] different behavior of which command in zsh and bash

查看:37
本文介绍了zsh 和 bash 中 which 命令的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 zsh 和 oh-my-zsh 框架一段时间,我注意到哪个命令在 zsh 中的行为与在 bash 中不同.

By using zsh for some time along with oh-my-zsh framework, I noticed that which command behaves different in zsh, than in bash.

我的意思:

# on zsh
ilias@ilias-pc ~  ➜ which ls
ls: aliased to ls --color=auto
ilias@ilias-pc ~  ➜ which which
which: shell built-in command
ilias@ilias-pc ~  ➜ bash
[ilias@ilias-pc ~]$ which ls
/usr/bin/ls
[ilias@ilias-pc ~]$ which which
/usr/bin/which
[ilias@ilias-pc ~]$ 

为什么会发生这种情况,我该如何修复"它?

Why does this happen and how can I "fix" it?

附注.我在 Arch Linux 上重现了这个(不确定它是否重要,但我提到了它).

PS. I reproduce this on Arch Linux (not sure whether it matters but I mention it).

推荐答案

$ zsh -c 'type which'
which is a shell builtin
$ bash -c 'type which'
which is /usr/bin/which

解决方案是不使用which(1),这是一个非标准且不是很有用的命令.您应该使用什么代替的问题并不是最简单的,因为替代方案指定不当且实施不一致,但它们比 which 好.

The solution is to not use which(1), which is a non-standard and not very useful command. The question of what you should use instead isn't the most straightforward due to the alternatives being poorly specified and inconsistently implemented, but they are better than which.

根据您的要求,您需要 command(参见 -v 选项)、typewhence.对于前两者,请参见 POSIX,对于后者,请参见 shell 手册.(Bash 不支持 wherece,但大多数其他 ksh 衍生产品都支持它,尽管不一致.它通常具有最多的功能).

Depending on your requirements, you want command (see the -v option), type, or whence. See POSIX for the former two, or your shell manual for the latter. (Bash doesn't support whence, but it is supported by most other ksh derivatives, albeit inconsistently. It typically has the most features).

这篇关于zsh 和 bash 中 which 命令的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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