在我的情况下,我希望保留`#!/bin/sh`作为shebang行,因为不是每个系统都有`/bin/bash`可用。
原始的Bourne shell可能不支持`$(...)`语法,但现在大多数`sh` shell都符合POSIX标准,
POSIX规范支持这种语法。例如,
- 在Ubuntu上,`/bin/sh`是`/bin/dash`。
- 在MacOS上,`/bin/sh`是`/bin/bash`。
- 在Alpine上,`/bin/sh`是`/bin/ash`。
所有这些都符合POSIX规范。传统上,如果我们想编写可移植的Shell脚本,我们应该将shebang行保留为`#!/bin/sh`。如果我们不打算使用任何Bashism,则不应该仅为了语法高亮而将其更改为`#!/bin/bash`。
好的,但是对于错误的红色高亮呢?问题在于Vim将
#!/bin/sh
解释为对
1979年最初的Bourne shell的引用,不支持
$(...)
。也许这是Vim向后兼容性的证明,或者可能是不太多的人关心。这里有一个描述相同行为的
相关GitHub问题。
无论如何,对我来说最好的解决方案是在我的配置文件中设置
let g:is_posix = 1
。有趣的是,如果你浏览Vim的运行时文件,它相当于设置
let g:is_kornshell = 1
。在同一文件的后面,当需要设置语法高亮时,还有
一些相关评论。
" $() and $(()): {{{1
" $(..) is not supported by sh (Bourne shell). However, apparently
" some systems (HP?) have as their /bin/sh a (link to) Korn shell
" (ie. Posix compliant shell). /bin/ksh should work for those
" systems too, however, so the following syntax will flag $(..) as
" an Error under /bin/sh. By consensus of vimdev'ers!
关于Bourne shell如何诞生,如何再次作为Ubuntu上/bin/sh的替代品而变成bash,并最终被dash所取代的简要有趣历史,可以在
https://askubuntu.com/a/976504找到。
TAB=$'\t'
在bash
中指定一个字面上的制表符。 - chepner