在#!/ bin / sh之后加破折号'-'-

Goi*_*Off 3 shell centos sh

我一直在CentOS 7上处理一些脚本,有时我看到:

#!/bin/sh -
Run Code Online (Sandbox Code Playgroud)

在第一行。查看手册页,sh我在Special Parameters

   -      Expands to the current option flags as  specified  upon  invocation,
          by  the  set  builtin  command, or those set by the shell
          itself (such as the -i option).
Run Code Online (Sandbox Code Playgroud)

这到底是什么意思?什么时候需要使用此特殊参数选项?

lar*_*sks 5

您正在阅读的文档与您正在查看的命令行无关:它是指特殊变量。在这种情况下,如果运行echo $-,将看到“调用时指定的当前选项标志...”。

如果看一下手册页的OPTIONS一部分bash,您会发现:

--       A -- signals the end of options and disables  further  option  processing.
         Any  arguments  after  the  -- are treated as filenames and arguments.  An
         argument of - is equivalent to --.
Run Code Online (Sandbox Code Playgroud)

换句话说,参数的-简单含义是“此参数之后没有其他选择”。

您经常会看到这种用法,在这种情况下,您希望避免将文件名开头-意外地视为命令选项:例如,如果-R当前目录中有一个文件名,则运行ls *实际上会ls -R产生并产生递归列表,而ls -- *不会对-R文件进行特殊处理。

#!在行中使用单破折号是为了安全起见。您可以在这里阅读更多有关它的信息