Documentation
¶
Overview ¶
Package syntax is a fork of mvdan.cc/sh/v3@v3.10.0/syntax.
Copyright (c) 2016, Daniel Martí. All rights reserved.
It is a reduced set of the package to only provide the Quote function, and contains the LICENSE, quote.go and parser.go files at the given revision.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsKeyword ¶
IsKeyword returns true if the given word is part of the language keywords.
func Quote ¶
func Quote(s string, lang LangVariant) (string, error)
Quote returns a quoted version of the input string, so that the quoted version is expanded or interpreted as the original string in the given language variant.
Quoting is necessary when using arbitrary literal strings as words in a shell script or command. Without quoting, one can run into syntax errors, as well as the possibility of running unintended code.
An error is returned when a string cannot be quoted for a variant. For instance, POSIX lacks escape sequences for non-printable characters, and no language variant can represent a string containing null bytes. In such cases, the returned error type will be *QuoteError.
The quoting strategy is chosen on a best-effort basis, to minimize the amount of extra bytes necessary.
Some strings do not require any quoting and are returned unchanged. Those strings can be directly surrounded in single quotes as well.
Types ¶
type LangVariant ¶
type LangVariant int
LangVariant describes a shell language variant to use when tokenizing and parsing shell code. The zero value is LangBash.
const ( // LangBash corresponds to the GNU Bash language, as described in its // manual at https://www.gnu.org/software/bash/manual/bash.html. // // We currently follow Bash version 5.2. // // Its string representation is "bash". LangBash LangVariant = iota // LangPOSIX corresponds to the POSIX Shell language, as described at // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html. // // Its string representation is "posix" or "sh". LangPOSIX // LangMirBSDKorn corresponds to the MirBSD Korn Shell, also known as // mksh, as described at http://www.mirbsd.org/htman/i386/man1/mksh.htm. // Note that it shares some features with Bash, due to the shared // ancestry that is ksh. // // We currently follow mksh version 59. // // Its string representation is "mksh". LangMirBSDKorn // LangBats corresponds to the Bash Automated Testing System language, // as described at https://github.com/bats-core/bats-core. Note that // it's just a small extension of the Bash language. // // Its string representation is "bats". LangBats // LangAuto corresponds to automatic language detection, // commonly used by end-user applications like shfmt, // which can guess a file's language variant given its filename or shebang. // // At this time, [Variant] does not support LangAuto. LangAuto )
func (LangVariant) String ¶
func (l LangVariant) String() string
Source Files
¶
- doc.go
- parser.go
- quote.go