shell - #!/bin/sh 之后的破折号'-' -
问题描述
我一直在 CentOS 7 上编写一些脚本,有时我会看到:
#!/bin/sh -
在第一行。查看手册页,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).
这到底是什么意思?我什么时候需要使用这个特殊的参数选项??
解决方案
您正在阅读的文档与您正在查看的命令行无关:它指的是特殊变量。在这种情况下,如果您运行,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 --.
换句话说,一个参数的-
简单意思是“在这个参数之后没有其他选项”。
您经常会看到这用于您希望避免以-
意外将文件名开头的文件名视为命令选项的情况:例如,如果-R
在当前目录中命名了一个文件,则运行ls *
实际上将表现为ls -R
并产生递归列表,而ls -- *
不会-R
特别对待文件。
在该#!
行中使用单破折号是为了安全防范。您可以在此处阅读更多相关信息。
推荐阅读
- amazon-web-services - AWS ioT:来自事物的用户连接(使用受限策略?)
- hibernate - jpa中的onetoonemapping,子表中存在外键
- elasticsearch - 使用 filebeat 记录 - 从多行 json 文件中提取 ECS 字段
- node.js - NodeJS:在调用函数之前等待创建变量
- prometheus - 格拉凡找不到普罗米修斯
- karate - 是否可以使用空手道 UI 触发悬停事件(mouseover/mouseenter)?
- c++ - 在 C++ 中使用递归合并排序算法
- docker - 如何设置环境变量并在单个 docker 命令中运行 gunicorn 命令?
- powershell - 传递参数的 Powershell 条件
- css - 100vh 后滚动更改导航栏颜色