Skip to content

shellish #

Shell-like Syntax Parser and Text Processor

Constants #

const safe_chars = '%+,-./0123456789:=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'

safe_chars contains ASCII characters that can be used in shell without any escaping.

const special_chars = ' \$"\'\\!'

special_chars contains ASCII characters that must be escaped in shell.

fn double_quote #

fn double_quote(s string) string

double_quote returns a " quoted version of string. In-string double quote will be escaped with \.

Note: Shell interprets special characters in string quoted by double quotes.To prevent this use quote() instead or use double_quote() in conjunction with escape().

Example

assert shellish.double_quote('NAME="Arch Linux"') == r'"NAME=\"Arch Linux\""'
assert shellish.double_quote(r'Hello, ${NAME}!') == r'"Hello, ${NAME}!"'

fn escape #

fn escape(s string) string

escape returns a shell-escaped version of string s. Shell special characters will be escaped with backslashes without inserting quotes.

Example

assert shellish.escape('Hello, World!') == r'Hello,\ World\!'

fn join #

fn join(a []string) string

join joins a array members into a shell command.

Example

assert shellish.join(['sh', '-c', 'hostname -f']) == 'sh -c 'hostname -f''

fn quote #

fn quote(s string) string

quote returns a quoted version of string s.

Note: String will be quoted with single quotes ('). If you expect ", use double_quote() instead.

Example

assert shellish.quote('Hello, world!') == ''Hello, world!''
assert shellish.quote('John's notebook') == '\'John\'"\'"\'s notebook\''

fn split #

fn split(s string) ![]string

split splits the s string into tokens using shell-like syntax in POSIX manner.

Example

assert shellish.split('echo "Hello, World!"') == ['echo', 'Hello, World!']

fn strip_ansi_escape_codes #

fn strip_ansi_escape_codes(s string) string

strip_ansi_escape_codes strips ANSI escape sequences starting with ESC [ from string.

fn strip_non_printable #

fn strip_non_printable(s string) string

strip_non_printable strips non-printable ASCII characters (from 0x00 to 0x1f and 0x7f) from string.

fn unescape #

fn unescape(s string) string

unescape unescapes the escaped string by removing backslash escapes.

Example

assert shellish.unescape(r'line\ with\ spaces\\n') == r'line with spaces\n'

fn unquote #

fn unquote(s string) string

unquote removes the leading and trailing quotes (any of ", ') from string.