我对编程有点陌生(不是真的,但我仍在学习 - 我们不是吗?)。虽然我了解 Java 和 Python,并且了解 C、C++、JS、C#、HTML、CSS 等(并且我可以在终端中很好地导航),但我不熟悉终端中的 $PATH 是什么。
我使用 Linux 终端和 Mac 终端的频率比以前要高得多(如果我两年前使用过的话),而且我知道对于 python,它希望你“导出”它的路径,例如 PATH=\path\to\python\bin:${PATH}\ export PATH
。然而,我什至不知道它是做什么的。我试图找出答案,但我只能找到人们说“导出这条路径并导出那个路径。”
那么,它是什么以及为什么使用它?我明白(如果你为 Python 这样做),它基本上使“python”(或“python2”或“python3”)成为一个变量,但我只是不明白它是什么的概念。
最佳答案
man bash
描述为:
PATH
block 引用>The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). A zero-length (null) directory name in the value of
PATH
indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon. The default path is system-dependent, and is set by the administrator who installs bash. A common value is/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin'
.当您运行命令时,例如
python
,操作系统尝试查找python
程序存储在PATH
中的目录列表中.假设您的
PATH
是/usr/local/bin:/foo:/bar:/baz:/usr/bin
。当您尝试运行python
时comamnd,操作系统将查找名为python
的可执行文件按顺序在这些目录中。在 Linux 上,您可以使用strace
观看它执行此操作。命令:$ PATH=/usr/local/bin:/foo:/bar:/baz:/usr/bin strace -f /bin/bash -c 'python --version' 2>&1 | grep 'stat.*python' stat("/usr/local/bin/python", 0x7fff98b63d00) = -1 ENOENT (No such file or directory) stat("/foo/python", 0x7fff98b63d00) = -1 ENOENT (No such file or directory) stat("/bar/python", 0x7fff98b63d00) = -1 ENOENT (No such file or directory) stat("/baz/python", 0x7fff98b63d00) = -1 ENOENT (No such file or directory) stat("/usr/bin/python", {st_mode=S_IFREG|0755, st_size=4864, ...}) = 0
尽快
python
位于/usr/bin/python
,搜索停止,程序运行。
关于path - 为什么使用 $PATH 以及它是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949636/