string::token::shell(3tcl)
β¨ string::token::shell β¨
okay bestie, so this is the manual page for string::token::shell, living over in the <pink>3tcl</pink> manual section π
it's part of the Tcl text and string utilities crowd, and it's literally just here to help you chop a shell command line into words. iconic behaviour tbh π
πΈ NAME πΈ
string::token::shell β parsing of shell command line. yes that's the whole name, short and sweet, main character energy π
π¦ SYNOPSIS π¦
package require Tcl 8.5 9
package require string::token::shell ?1.3?
package require string::token ?1?
package require fileutil
::string token shell ?-indices? ?-partial? ?--? string
so yeah, you gotta pull in Tcl 8.5 9, then string::token::shell (version 1.3 is the optional vibe), plus string::token (version 1), plus fileutil, and then you get the one glorious command β¨
π DESCRIPTION π
ngl this package gives you exactly one command, and what it does is parse a line of text using basic sh-syntax and hand you back a list of words. that's it. that's the whole thing. slay π«Ά
the complete set of procedures is described below β and yes, it's a set of one, we love a minimalist queen.
::string token shell ?-indices? ?-partial? ?--? string
this command parses the input string, assuming it's following basic sh-syntax. the result? a list of words in the string. periodt. π
if the input doesn't follow the allowed syntax, it throws an error β so no freestyling, bestie, the parser has standards. the behaviour can be tweaked by specifying either of the two options -indices and -partial.
--β when you drop this, option parsing stops right there at that point. you need this if the input string might start with a dash. in other words, this is pretty much required ifstringis user input. do not skip it, i'm begging π-indicesβ when specified, the output is not a list of words, but a list of 4-tuples describing the words. each tuple has the type of the word, its start- and end-indices in the input, and the actual text of the word.
heads up: the length of the word as given by the indices can differ from the length of the word found in the last element of the tuple π the indices describe the word's extent in the input, including delimiters, intra-word quoting, etc., whereas for the actual text of the word, delimiters are stripped, intra-word quoting is decoded, etc.
the possible token types are:
PLAINβ plain word, not quoted.D:QUOTEDβ word is delimited by double-quotes.S:QUOTEDβ word is delimited by single-quotes.D:QUOTED:PARTS:QUOTED:PARTβ like the previous types, but the word has no closing quote, i.e. it's incomplete. these token types can occur if and only if the option-partialwas specified, and only for the last word of the result. if-partialwas not specified, such incomplete words cause the command to throw an error instead π
-partialβ when specified, the parser will accept an incomplete quoted word (i.e. without a closing quote) at the end of the line as valid, instead of throwing an error. very forgiving of it π₯Ί
the basic shell syntax accepted here is unquoted, single- and double-quoted words, separated by whitespace. leading and trailing whitespace are possible too, and get stripped. shell variables in their various forms are not recognized, and neither are sub-shells β don't even try it. as for the recognized forms of words, see below for the detailed specification π
single-quoted word
a single-quoted word begins with a single-quote character, i.e. ' (ASCII 39), followed by zero or more unicode characters that are not a single-quote, and then closed by a single-quote.
the word must be followed by either the end of the string, or whitespace. a word cannot directly follow the word. (yes, that's what it says, and we're keeping it exactly like that π)
double-quoted word
a double-quoted word begins with a double-quote character, i.e. " (ASCII 34), followed by zero or more unicode characters that are not a double-quote, and then closed by a double-quote.
contrary to single-quoted words, a double-quote can be embedded into the word, by prefacing it β i.e. escaping it, i.e. quoting it β with a backslash character \ (ASCII 92). similarly, a backslash character must be quoted with itself to be inserted literally. yep, backslash inception, we love to see it β¨
unquoted word
unquoted words are not delimited by quotes, and thus cannot contain whitespace or single-quote characters. double-quote and backslash characters can be put into unquoted words by quoting them like for double-quoted words. no cap.
whitespace
whitespace is any unicode space character. this is equivalent to string is space, or the regular expression \s.
whitespace may occur before the first word, or after the last word. whitespace must occur between adjacent words β no running your words together, they need their personal space π«Ά
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. (their words, not mine, but honestly? same.)
please report such in the category textutil of the Tcllib Trackers: [http://core.tcl.tk/tcllib/reportlist]
please also report any ideas for enhancements you may have for either the package and/or the documentation β they wanna hear it, bestie π
when proposing code changes, please provide unified diffs, i.e. the output of diff -u. and note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. lowkey specific, but okay, we move π
π KEYWORDS π
bash, lexing, parsing, shell, string, tokenization
π CATEGORY π
Text processing
okay and that's the whole page!! this was the <hotpink>tcllib</hotpink> version <hotpink>1.3</hotpink> of string::token::shell(3tcl) β thanks for reading all the way to the end, you're a real one π go parse some shell lines, you absolute legend π¦β¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms