virtualenvwrapper.sh中的"$(command \which python)"是什么意思?

发布于 2022-09-04 02:08:36 字数 1340 浏览 8 评论 0

描述问题

意思: 获取Python的位置
获取Python路径,应该有几种写法,自己一下想到的是which,怎么也不会想到command这种命令(事实上,很少见到这个命令),然考虑到写这个脚本(virtualenvwrapper.sh)的是大神,必然shell编程经验比我丰富,踩过的坑比我多,这样写必然有其道理, 所以这道理是啥?

具体地,我将问题拆解成以下几个:

  1. \which是啥?

  2. $(which python)不可以吗?

  3. command是一个啥命令? (man command没有输出)

上下文环境

Linux
Python2.7

重现

virtualenvwrapper是常用的Python包,它是利用了shell包装了virtualenv,所以我想看看它的shell实现
然发现,有些部分不是很懂

要重现,可以去看virtualenvwrapper.sh这个脚本
注意: 在不同Linux发行版,不同shell(bash/zsh)程序中的位置不同(具体见官方手册)

相关代码

摘取部分代码


# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ]
then
    VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi

# Set the name of the virtualenv app to use.
if [ "$VIRTUALENVWRAPPER_VIRTUALENV" = "" ]
then
    VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
fi

# Set the name of the virtualenv-clone app to use.
if [ "$VIRTUALENVWRAPPER_VIRTUALENV_CLONE" = "" ]
then
    VIRTUALENVWRAPPER_VIRTUALENV_CLONE="virtualenv-clone"
fi

已经尝试哪些方法仍然没有解决(附上相关链接)

Google了:

  1. command \which python

问题简化

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

灯下孤影 2022-09-11 02:08:36

没有任何道理,可能这个脚本的作者是个老古董,不习惯bash吧。

GRAY°灰色天空 2022-09-11 02:08:36

作者在这一段注释中已经说明了,目的是为了多平台兼容

# Portable shell scripting is hard, let's go shopping.
#
# People insist on aliasing commands like 'cd', either with a real
# alias or even a shell function. Under bash and zsh, "builtin" forces
# the use of a command that is part of the shell itself instead of an
# alias, function, or external command, while "command" does something
# similar but allows external commands. Under ksh "builtin" registers
# a new command from a shared library, but "command" will pick up
# existing builtin commands. We need to use a builtin for cd because
# we are trying to change the state of the current shell, so we use
# "builtin" for bash and zsh but "command" under ksh.

bash 文档中的解释:

command
    command [-pVv] command [arguments …]
Runs command with arguments ignoring any shell function named command. 
Only shell builtin commands or commands found by searching
the PATH are executed. If there is a shell function named ls, running
‘command ls’ within the function will execute the external command ls
instead of calling the function recursively. The -p option means to
use a default value for PATH that is guaranteed to find all of the
standard utilities. The return status in this case is 127 if command
cannot be found or an error occurred, and the exit status of command
otherwise.
If either the -V or -v option is supplied, a description of command is 
printed. The -v option causes a single word indicating the
command or file name used to invoke command to be displayed; the -V
option produces a more verbose description. In this case, the return
status is zero if command is found, and non-zero if not.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文