bash:“哪个adb";不返回任何内容,但返回"command -v adb";返回我的期望 [英] bash: "which adb" returns nothing, but "command -v adb" returns what i'd expect
问题描述
这是一个让我烦恼的怪癖.
This is a bash weirdness that's been bugging me.
我在OSX上.
我已经安装了android SDK,因此adb
工具位于我的主目录中的文件夹中.该文件夹显示在我的路径中,由env
报告为~/Development/android_sdk_latest/platform-tools
.
I've installed the android SDK, and as a result the adb
tool is located in a folder in my home directory. That folder appears in my path as reported by env
as ~/Development/android_sdk_latest/platform-tools
.
adb
本身运行正常,但是当我执行which adb
时,结果为空.如果我执行command -v adb
,则结果是完整路径,如预期的那样:/Users/me/Development/android_sdk_latest/platform-tools/adb
adb
itself runs just fine, but when I do which adb
the result is empty. If I do command -v adb
, the result is the full path, as expected: /Users/me/Development/android_sdk_latest/platform-tools/adb
adb
没有出现在我的别名中.
adb
does not appear in my aliases.
bash路径或which
有什么微妙的地方吗?
What subtlety of bash paths or which
am I in the dark on?
推荐答案
您在/bin/bash
和which
命令之间遇到了微妙的不兼容性.
You've run into a subtle incompatibility between /bin/bash
and the which
command.
在我的系统(Linux Mint)上,which
命令实际上是一个Shell脚本,而不是内置命令,它的第一行是#! /bin/sh
.这意味着它使用/bin/sh
对$PATH
变量的处理.
On my system (Linux Mint), the which
command is actually a shell script, not a built-in command, and its first line is #! /bin/sh
. That means that it uses /bin/sh
's handling of the $PATH
variable.
这可能取决于/bin/sh
的设置方式(有时是指向/bin/bash
的符号链接),但是一些实验表明bash
处理$PATH
中的文字~
字符就像主目录的完整路径,但/bin/sh
则没有.既然你有
This can vary depending on how /bin/sh
is set up (it's sometimes a symlink to /bin/bash
), but a little experimentation shows that bash
handles a literal ~
character in $PATH
as if it were the full path to your home directory, but /bin/sh
does not. Since you have
~/Development/android_sdk_latest/platform-tools
作为$PATH
的元素之一,bash
(您的交互式外壳)可以找到adb
命令,但是sh
(由which
使用的外壳)找不到.
as one of the elements of your $PATH
, bash
(your interactive shell) can find the adb
command, but sh
(the shell used by which
) cannot.
在某些系统上,显然包括OSX系统,which
是二进制可执行文件.同样,由于它不是bash脚本,因此不会与bash对$PATH
的处理相匹配.
On some systems, apparently including your OSX system, which
is a binary executable. Again, since it's not a bash script, it's not going to match bash's treatment of $PATH
.
我建议进行两项更改.
首先,不要在您的$PATH
中放置文字~
.例如,要将platform-tools
目录附加到您的$PATH
,而不是此文件:
First, don't put a literal ~
in your $PATH
. For example, to append the platform-tools
directory to your $PATH
, rather than this:
export PATH="$PATH:~/Development/android_sdk_latest/platform-tools" # BAD!
执行以下操作:
export PATH="$PATH:$HOME/Development/android_sdk_latest/platform-tools"
$HOME
将扩展到主目录的路径(运行export
命令时,而不是稍后使用$PATH
时). ~
不能在双引号字符串内扩展.
The $HOME
will expand to the path to your home directory (when you run the export
command, not when you use $PATH
later). ~
is not expanded within double-quoted strings.
第二,而不是使用which
命令,而是使用bash
内置的type
命令. type
将遵循当前shell的规则,而不是/bin/sh
的规则,并且它将能够报告which
无法看到的shell函数和别名.它有几个有用的命令行选项.在bash提示符下键入help type
以获得详细信息.
Second, rather than using the which
command, use the type
command that's built into bash
. type
will follow the rules of your current shell rather than those of /bin/sh
, and it will be able to report shell functions and aliases that which
is unable to see. It has several useful command-line options; type help type
at a bash prompt for details.
bash中记录了bash外壳对$PATH
中的~
字符的处理.手册中关于.
The bash shell's treatement of ~
characters in $PATH
is documented in the bash manual's section on Tilde Expansion.
这篇关于bash:“哪个adb";不返回任何内容,但返回"command -v adb";返回我的期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!