了解 PATH 环境变量的工作原理

了解 PATH 环境变量的工作原理

我遵循了许多指南和书籍来了解PATH变量概念,但没有一个能“完整”地解释它在 Linux 中的工作原理。

例如,如果我在 Bash 中设置如下路径:

export PATH="$VIRTUALENVWRAPPER_PYTHON:/home/ujjval/anaconda2/bin/python:/bin:/usr/bin"

VIRTUALENVWRAPPER_PYTHON它设置了一个名为给定目录的变量的路径,对吗?

例如,如果我这样做

export PATH="/usr/bin:bin:PATH"

PATH 的先前值会被覆盖吗?另外,:PATH最后的 是什么意思?

除此之外,如果我在我的./bashrc文件中写入

export PATH="$VIRTUALENVWRAPPER_PYTHON:/home/ujjval/anaconda2/bin/python:/bin:/usr/bin"

和上面两种设置路径的方法有什么区别吗?

答案1

export PATH="$VIRTUALENVWRAPPER_PYTHON:/home/ujjval/anaconda2/bin/python:/bin:/usr/bin"

设置您当前的 shell,使用当时 PATH的值。$VIRTUALENVWRAPPER_PYTHON

export PATH="/usr/bin:bin:PATH"

打破了你的PATH(在哪里bin?是什么PATH)。它应该是:

export PATH="/usr/bin:/bin:$PATH"

使用 的先前值PATH

是的,后续分配将PATH覆盖先前的值。

PATH="$VIRTUALENVWRAPPER_PYTHON:/home/ujjval/anaconda2/bin/python:/bin:/usr/bin"

将您的设置PATH为当前的值VIRTUALENVWRAPPER_PYTHON、一个可能的单个文件(这是错误的 - 是/home/ujjval/anaconda2/bin/python文件还是目录?应该是吗/home/ujjval/anaconda2/bin?)和两个系统目录,忽略先前的值PATH

忽略前者PATH是不安全的。例如,Ubuntu 在开始支持 Snap 时将其添加/snap/bin到默认设置中PATH。(请参阅/etc/profile.d/apps-bin-path.sh)。您的方法将失去对该目录的跟踪。

以下是PATH(用冒号分隔的目录列表)的工作原理(来自man bash

COMMAND EXECUTION

       After a command has been split into words, if it  results  in  a  simple  command  and  an
       optional list of arguments, the following actions are taken.

       If the command name contains no slashes, the shell attempts to locate it.  If there exists
       a shell function by that name, that function is invoked as described above  in  FUNCTIONS.
       If  the  name  does  not  match a function, the shell searches for it in the list of shell
       builtins.  If a match is found, that builtin is invoked.

       If the name is neither a shell function nor a  builtin,  and  contains  no  slashes,  bash
       searches  each  element  of the PATH for a directory containing an executable file by that
       name.  Bash uses a hash table to remember the full pathnames of executable files (see hash
       under  SHELL  BUILTIN  COMMANDS  below).   A  full  search  of  the directories in PATH is
       performed only if the command  is  not  found  in  the  hash  table.   If  the  search  is
       unsuccessful,    the    shell    searches    for    a   defined   shell   function   named
       command_not_found_handle.  If that function  exists,  it  is  invoked  with  the  original
       command  and  the  original  command's arguments as its arguments, and the function's exit
       status becomes the exit status of the shell.  If that function is not defined,  the  shell
       prints an error message and returns an exit status of 127.

       If  the  search  is  successful,  or if the command name contains one or more slashes, the
       shell executes the named program in a separate execution environment.  Argument 0  is  set
       to  the  name  given,  and the remaining arguments to the command are set to the arguments
       given, if any.

       If this execution fails because the file is not in executable format, and the file is  not
       a  directory,  it  is  assumed  to be a shell script, a file containing shell commands.  A
       subshell is spawned to execute it.  This subshell reinitializes itself, so that the effect
       is  as  if  a new shell had been invoked to handle the script, with the exception that the
       locations of commands remembered by  the  parent  (see  hash  below  under  SHELL  BUILTIN
       COMMANDS) are retained by the child.

       If  the  program is a file beginning with #!, the remainder of the first line specifies an
       interpreter for the program.  The shell executes the specified  interpreter  on  operating
       systems  that  do  not  handle  this  executable  format themselves.  The arguments to the
       interpreter consist of a single optional argument following the interpreter  name  on  the
       first  line  of  the program, followed by the name of the program, followed by the command
       arguments, if any

相关内容