bash PATH 中的冒号有什么作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35737627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:19:38  来源:igfitidea点击:

What does the colon do in PATH

bashterminal

提问by Kabe Lee

I am new to bash, and I saw people often add :after a directory when modifying PATH. After searching for a while, I did not find an answer for that, or I believe I did not search it correctly. So I hope I could get an answer here.

我是 bash 新手,我看到人们:在修改PATH. 搜索了一段时间后,我没有找到答案,或者我相信我没有正确搜索它。所以我希望我能在这里得到答案。

Example:

例子:

/Users/chengluli/anaconda/bin:/Users/chengluli/.rbenv/shims:/

What does the :after binand shimsdo?

是什么在:以后binshims做?

回答by slashfoo

:is the separator. The PATHvariable is itself a list of folders that are "walked" through when you run a command.

:是分隔符。该PATH变量本身就是一个在您运行命令时“走过”的文件夹列表。

In this case, the folders on your PATHare:

在这种情况下,您的文件夹PATH是:

  • /Users/chengluli/anaconda/bin
  • /Users/chengluli/.rbenv/shims
  • /
  • /Users/chengluli/anaconda/bin
  • /Users/chengluli/.rbenv/shims
  • /

回答by cdarke

As others have said, the :is a separator (Windows uses a semi-colon ;). But you are probably thinking of a trailing colon :at the endof the PATHvariable. For example:

正如其他人所说,:is 是一个分隔符(Windows 使用分号;)。但你可能在想尾随冒号:结束了的PATH变量。例如:

/Users/chengluli/anaconda/bin:/Users/chengluli/.rbenv/shims:

From the bash man pages:

从 bash 手册页:

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.

PATH 值中的零长度(空)目录名称表示当前目录。空目录名称可能显示为两个相邻的冒号,或者显示为首字母或尾随冒号。

Putting the current directory in the PATH is generally considered a security risk and a bad idea. It is particularly dangerous when using the root user.

将当前目录放在 PATH 中通常被认为是一个安全风险和一个坏主意。使用 root 用户时尤其危险。

By the way, bash only uses $PATH on the first call of an external program, after that it uses a hash table. See man bashand the hashcommand

顺便说一下,bash 只在第一次调用外部程序时使用 $PATH,之后它使用哈希表。查看man bashhash命令

回答by Huihoo

If you run ls -l 123at the command line, you are telling bash to find the command called lsin the filesystem. However, lsis just a file name, bash needs the absolute path of lsin the filesystem. So bash searches for a file called lsin a list of default directories, one by one in order.

如果您ls -l 123在命令行中运行,则是在告诉 bash 查找ls在文件系统中调用的命令。然而,ls只是一个文件名,bash需要ls文件系统中的绝对路径。因此 bash 会ls按顺序一个一个地搜索默认目录列表中调用的文件。

A list of default directories is stored in the PATHvariable, separated by :.

默认目录列表存储在PATH变量中,以:.

回答by gzh

Th quotation from output of man bashcommand

来自man bash命令输出的引用

PATH
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 sys‐tem-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''.

PATH
命令的搜索路径。它是一个以冒号分隔的目录列表,shell 在其中查找命令(请参阅下面的命令执行)。PATH 值中的零长度(空)目录名称表示当前目录。空目录名称可能显示为两个相邻的冒号,或者显示为首字母或尾随冒号。默认路径依赖于系统,由安装 bash 的管理员设置。一个常见的值是“/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin”。

If you have questions about bash script or environment variable, please use man bashfirstly.

如果您对 bash 脚本或环境变量有疑问,请先使用man bash