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

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

问题描述

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

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

$ 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 中,对于函数 type -a 也会显示函数定义.你可以使用 declare -f functionname 来做同样的事情(你必须对 zsh 使用它,因为 type -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天全站免登陆