alessio/shellescape
GitHub: alessio/shellescape
shellescape 是一个 Go 语言库,用于对任意字符串进行 shell 转义,使其安全地用作命令行参数。
Stars: 201 | Forks: 19
# shellescape

[](https://pkg.go.dev/github.com/alessio/shellescape?tab=overview)
[](https://sourcegraph.com/github.com/alessio/shellescape)
[](https://codecov.io/gh/alessio/shellescape)
[](https://goreportcard.com/report/github.com/alessio/shellescape)
对任意字符串进行转义,以安全地用作命令行参数。
## 包内容
此包提供了 `shellescape.Quote()` 函数,该函数返回字符串的 shell 转义副本。当已知 Go 程序的输出将被附加到/用于 shell 程序的命令行参数上下文中时,此功能会非常有用。
此项目受到了 Python 原始包 [shellescape](https://pypi.python.org/pypi/shellescape) 的启发。
## 用法
以下代码片段展示了一个典型的不安全用法:
```
package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf("ls -l %s\n", os.Args[1])
}
```
_[在 Go Playground 中查看](https://play.golang.org/p/Wj2WoUfH_d)_
特别是在创建可能最终由 shell 解释器执行的命令 pipeline 时,不对参数进行转义是极其不安全的。
`shellescape.Quote()` 派上了用场,可以安全地转义字符串:
```
package main
import (
"fmt"
"os"
"al.essio.dev/pkg/shellescape"
)
func main() {
fmt.Printf("ls -l %s\n", shellescape.Quote(os.Args[1]))
}
```
_[在 Go Playground 中查看](https://go.dev/play/p/GeguukpSUTk)_
## escargs 工具
__escargs__ 从标准输入读取行,并打印 shell 转义后的版本。与 __xargs__ 不同,标准输入上的空行不会被丢弃。
标签:EVTX分析, Go, Ruby工具, Shell转义, 字符串转义, 日志审计