con*_*are 42 script bash shell
我有一些主要使用的 bash 脚本
#!/bin/bash
Run Code Online (Sandbox Code Playgroud)
但我经常遇到一些看起来像
#!/bin/bash -e
#!/bin/bash -x
#!/bin/bash -ex
Run Code Online (Sandbox Code Playgroud)
等等。
有人可以解释这些shebang选项的含义和好处以及它们是否适用于其他shebang吗?
Gil*_*il' 45
如果脚本/path/to/foo
以 开头#!/bin/bash
,则执行/path/to/foo arg1 arg2
等同于执行/bin/bash /path/too/foo arg1 arg2
。如果 shebang 行是#!/bin/bash -ex
,则相当于执行/bin/bash -ex /path/too/foo arg1 arg2
. 此功能由内核管理。
请注意,您可以在 shebang 行上只使用一个参数:一些 unices(例如 Linux)只接受一个参数,因此这#!/bin/bash -e -x
将导致 bash 接收单个五个字符的参数-e -x
(语法错误)而不是两个参数-e
和-x
.
对于 Bourne shellsh
和派生的 shell,例如 POSIX sh、bash、ksh 和 zsh:
-e
意味着如果任何命令失败(通过返回非零状态表示),脚本将立即终止。-x
导致外壳打印执行跟踪。其他程序可能理解这些选项,但具有不同的含义。
cYr*_*rus 24
它们是传递给bash
查看help set
更多信息的选项,在这种情况下:
-x Print commands and their arguments as they are executed.
-e Exit immediately if a command exits with a non-zero status.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27429 次 |
最近记录: |