“. ./”在 Linux shell 中是什么意思?

0 command-line

我有一个不执行./script.sh但需要. ./script.sh执行的 shell 脚本 。

./和 和有. ./什么区别?我在 Windows 上使用 MSYS2 环境。请注意点之间的空间。我知道什么../,做什么和那不能解决问题,因为我与我的可执行文件在同一目录中。

这是输出:

终端输出

Ark*_*zyk 7

. 是 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.
Run Code Online (Sandbox Code Playgroud)

这样做总是更安全. ./script. script因为. 默认情况下搜索 PATH 并且您可能会遇到名称冲突:

$ echo echo hi > script
$ . script
bash: .: /usr/bin/script: cannot execute binary file
$ . ./script
hi
Run Code Online (Sandbox Code Playgroud)

并且因为某些 shell 不搜索当前目录是默认的:

$ mv script script-to-be-sourced
$ dash
$ . script-to-be-sourced
dash: 1: .: script-to-be-sourced: not found
Run Code Online (Sandbox Code Playgroud)