为什么在我应该执行的某些命令的开头有额外的点

Nep*_*daj 5 linux shell

我下载了一个weblogic server的安装包,在README里面,有这个命令要执行:

Linux/Mac
$ . ./configure.sh
Run Code Online (Sandbox Code Playgroud)

这不是我第一次看到这个。为什么在命令的开头有一个额外的点?当我只做时./configure.sh,结果是一样的

slm*_*slm 6

点 ( .) 是用于从文件执行命令的符号,它作为点的参数给出。这个文件的内容,比如说./configure.sh,在当前的 shell 中执行。dot 命令起源于 Bourne shell,在其他 shell 中也可用,例如在 Bash 中。

摘自 Bash 的手册页

    .  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, file names 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 posi? tional 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.
Run Code Online (Sandbox Code Playgroud)

注意:其他 shellcsh有类似的命令source,许多更现代的变体同时支持点符号和source命令。Bash 实际上支持两者。

例子

这是一个示例,我们将$SOMEVAR通过获取定义了该变量的文件来在当前 shell 中设置变量。

这是示例文件:

$ cat test.sh 
SOMEVAR="hi"
Run Code Online (Sandbox Code Playgroud)

首先,我们检查以确保该变量$SOMEVAR尚未在当前 shell 中设置。

$ echo $SOMEVAR

$
Run Code Online (Sandbox Code Playgroud)

现在我们获取它,并确认它现在已经设置:

$ . ./test.sh 
$ echo $SOMEVAR
hi
Run Code Online (Sandbox Code Playgroud)

可移植性

感谢@ChrisDown 提到这一点。点 ( .) 被指定为 POSIX 的一部分,因此是可移植的,而命令source不是。请参阅The Open Group Base Specifications Issue 7文档中的此处:“2. Shell 命令语言”。本节具体。

摘抄

姓名

dot - 在当前环境中执行命令

概要

. 文件

描述

shell 应从当前环境中的文件执行命令。

如果文件不包含 ,shell 将使用 PATH 指定的搜索路径来查找包含文件的目录。然而,与普通命令搜索不同,dot 实用程序搜索的文件不需要是可执行的。如果没有找到可读文件,非交互式 shell 将中止;交互式 shell 应将诊断消息写入标准错误,但这种情况不应被视为语法错误。