Shell - 如何查找某个命令的目录? [英] Shell - How to find directory of some command?

查看:609
本文介绍了Shell - 如何查找某个命令的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当你在shell上时,可以使用的唯一命令是可以在PATH上设置的某个目录中找到的命令。
即使我不知道如何看到我的PATH变量(这是另一个很好的问题,可以回答)的dirs,我想知道的是:

I know that when you are on shell, the only commands that can be used are the ones that can be found on some directory set on PATH. Even I don't know how to see what dirs are on my PATH variable (and this is another good question that could be answered), what I'd like to know is:

我来到shell并写:

I come to shell and write:

$ lshw

我想知道一个shell命令,可以告诉我这个命令的位置。换句话说,这个可执行文件位于哪里?

I want to know a command on shell that can tell me WHERE this command is located. In other words, where this "executable file" is located?

像这样:

$ location lshw
/usr/bin

推荐答案

如果您使用Bash或zsh,请使用:

If you're using Bash or zsh, use this:

type -a lshw

是内置函数,函数,别名或外部可执行文件。如果是后者,它将显示它出现在 PATH 中的每个地方。

This will show whether the target is a builtin, a function, an alias or an external executable. If the latter, it will show each place it appears in your PATH.

bash$ type -a lshw
lshw is /usr/bin/lshw
bash$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
bash$ zsh
zsh% type -a which
which is a shell builtin
which is /usr/bin/which

在Bash中,对于类型-a ,还将显示函数定义。你可以使用 declare -f functionname 做同样的事情(你必须使用zsh,因为类型-a 不)。

In Bash, for functions type -a will also display the function definition. You can use declare -f functionname to do the same thing (you have to use that for zsh, since type -a doesn't).

这篇关于Shell - 如何查找某个命令的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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