答案1
.
是 Bash 和其他多个 POSIX shell 中的源运算符:
$ help .
.: . filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
. ./script
这样做总是更安全,. script
因为.
默认情况下搜索 PATH,并且您可能会遇到名称冲突:
$ echo echo hi > script
$ . script
bash: .: /usr/bin/script: cannot execute binary file
$ . ./script
hi
并且因为有些 shell 默认不搜索当前目录:
$ mv script script-to-be-sourced
$ dash
$ . script-to-be-sourced
dash: 1: .: script-to-be-sourced: not found
答案2
两者完全不同,无法比较。
./
表示当前目录。
. ./
没有任何意义,运行会打印以下错误:
-bash: .: ./: is a directory
然而,它需要被分解成两部分:
- 命令
.
- 参数
./
该.
命令是 shell 内置命令,与该命令同义source
。它以文件而非目录作为参数,因此会出现上述错误。
您可能见过的一个非常命令的示例是以下命令:
$ . ~/.bashrc
这正是
$ source ~/.bashrc
你可以找到他们的手动的通过运行
man bash-builtins
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell environment and return
the exit status of the last command executed from filename. If filename does not
contain a slash, filenames in PATH are used to find the directory containing
filename. The file searched for in PATH need not be executable. When bash is not
in posix mode, the current directory is searched if no file is found in PATH. If
the sourcepath option to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the positional parameters
when filename is executed. Otherwise the positional parameters are unchanged. The
return status is the status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or cannot be read.