bash

Bash and other UNIX shell (sh) syntax.

In this module:

Language

Name (Aliases)

Description

Filename(s)

Mime Type(s)

Bash

Bash (sh)

Bash and other shell language

*.sh, *.bash

text/x-shellscript

class Bash[source]

Bases: parce.language.Language

Bash and other shell syntax.

root

Find one or mode command lines.

This lexicon is derived with the special character ` if called from the backtick() lexicon, or with `)` from the subshell() lexicon.

command

Find commands and arguments, pops back on line end or when ARG is ahead.

arguments

Arguments after a command, called from root.

classmethod common()[source]

Yield common stuff: comment, expression, expansions, etc.

classmethod numeric_common()[source]

Nummeric values.

classmethod expression_common()[source]

Common things in expressions.

classmethod substitution()[source]

Variable expansion with $.

classmethod quoting()[source]

Escape, single and double quotes.

brace_expansion

Used to parse a brace expansion.

classmethod make_heredoc_regex(m)[source]

Make a regular expression to terminate the here doc with.

The returned pattern is used to terminate the both here_document lexicons with.

here_document

A here document that is expanded, terminated by ARG.

here_document_quoted

A here document that’s not expanded, terminated by ARG.

here_string

A here-string, the text after <<<.

assignment

An assignment, the text after =.

dqstring

A double-quoted string.

sqstring

A single-quoted string.

escape_string

A single-quoted string.

backtick

Stuff between ``.

parameter

Contents of ${}.

subscript

Contents of [] in an array reference.

subshell

A subshell ().

group_command

A group command { ...; }.

let_expr

An expression after let.

arith_expr

An arithmetic expression (()).

cond_expr

A conditional expression [[]].

test_expr

A test expression [].

comment

A comment.

Example:

Root lexicon Bash.root and text:

Text rendered using default theme

#!/bin/sh

uri=$1

col=${uri##*:}  uri=${uri%:*}
char=${uri##*:} uri=${uri%:*}
line=${uri##*:} uri=${uri%:*}
proto=${uri%%:*}
file="file:${uri#*:}"

if kde-config --version |grep -q 'KDE: [23]'
then
        # for KDE 3.x:
        let line=--line
else
        # for KDE 4.x:
        let col=++col
fi

exec kate --use --line $line --column $col "$file"

# here is a here document
cat <<EOF
blabla blabla
blabla $variable, $(command)
EOF

# and here is another, quoted.
cat <<-"EOF"
  blabla blabla
  blabla $variable, $(command)
   EOF

Result tree:

<Context Bash.root at 0-529 (23 children)>
 ├╴<Token '#!/bin/sh' at 0:9 (Comment.Special)>
 ├╴<Context Bash.command at 11-17 (3 children)>
 │  ├╴<Token 'uri' at 11:14 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 14:15 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 15-17 (1 child)>
 │     ╰╴<Token '$1' at 15:17 (Name.Variable.Special)>
 ├╴<Context Bash.command at 19-48 (6 children)>
 │  ├╴<Token 'col' at 19:22 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 22:23 (Delimiter.Operator.Assignment)>
 │  ├╴<Context Bash.assignment at 23-33 (3 children)>
 │  │  ├╴<Token '${' at 23:25 (Name.Variable)>
 │  │  ├╴<Token 'uri' at 25:28 (Name.Variable)>
 │  │  ╰╴<Context Bash.parameter at 28-33 (4 children)>
 │  │     ├╴<Token '##' at 28:30 (Delimiter.ModeChange)>
 │  │     ├╴<Token '*' at 30:31 (Text.Template)>
 │  │     ├╴<Token ':' at 31:32 (Delimiter.ModeChange)>
 │  │     ╰╴<Token '}' at 32:33 (Name.Variable)>
 │  ├╴<Token 'uri' at 35:38 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 38:39 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 39-48 (3 children)>
 │     ├╴<Token '${' at 39:41 (Name.Variable)>
 │     ├╴<Token 'uri' at 41:44 (Name.Variable)>
 │     ╰╴<Context Bash.parameter at 44-48 (4 children)>
 │        ├╴<Token '%' at 44:45 (Delimiter.ModeChange)>
 │        ├╴<Token ':' at 45:46 (Delimiter.ModeChange)>
 │        ├╴<Token '*' at 46:47 (Text.Template)>
 │        ╰╴<Token '}' at 47:48 (Name.Variable)>
 ├╴<Context Bash.command at 49-78 (6 children)>
 │  ├╴<Token 'char' at 49:53 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 53:54 (Delimiter.Operator.Assignment)>
 │  ├╴<Context Bash.assignment at 54-64 (3 children)>
 │  │  ├╴<Token '${' at 54:56 (Name.Variable)>
 │  │  ├╴<Token 'uri' at 56:59 (Name.Variable)>
 │  │  ╰╴<Context Bash.parameter at 59-64 (4 children)>
 │  │     ├╴<Token '##' at 59:61 (Delimiter.ModeChange)>
 │  │     ├╴<Token '*' at 61:62 (Text.Template)>
 │  │     ├╴<Token ':' at 62:63 (Delimiter.ModeChange)>
 │  │     ╰╴<Token '}' at 63:64 (Name.Variable)>
 │  ├╴<Token 'uri' at 65:68 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 68:69 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 69-78 (3 children)>
 │     ├╴<Token '${' at 69:71 (Name.Variable)>
 │     ├╴<Token 'uri' at 71:74 (Name.Variable)>
 │     ╰╴<Context Bash.parameter at 74-78 (4 children)>
 │        ├╴<Token '%' at 74:75 (Delimiter.ModeChange)>
 │        ├╴<Token ':' at 75:76 (Delimiter.ModeChange)>
 │        ├╴<Token '*' at 76:77 (Text.Template)>
 │        ╰╴<Token '}' at 77:78 (Name.Variable)>
 ├╴<Context Bash.command at 79-108 (6 children)>
 │  ├╴<Token 'line' at 79:83 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 83:84 (Delimiter.Operator.Assignment)>
 │  ├╴<Context Bash.assignment at 84-94 (3 children)>
 │  │  ├╴<Token '${' at 84:86 (Name.Variable)>
 │  │  ├╴<Token 'uri' at 86:89 (Name.Variable)>
 │  │  ╰╴<Context Bash.parameter at 89-94 (4 children)>
 │  │     ├╴<Token '##' at 89:91 (Delimiter.ModeChange)>
 │  │     ├╴<Token '*' at 91:92 (Text.Template)>
 │  │     ├╴<Token ':' at 92:93 (Delimiter.ModeChange)>
 │  │     ╰╴<Token '}' at 93:94 (Name.Variable)>
 │  ├╴<Token 'uri' at 95:98 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 98:99 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 99-108 (3 children)>
 │     ├╴<Token '${' at 99:101 (Name.Variable)>
 │     ├╴<Token 'uri' at 101:104 (Name.Variable)>
 │     ╰╴<Context Bash.parameter at 104-108 (4 children)>
 │        ├╴<Token '%' at 104:105 (Delimiter.ModeChange)>
 │        ├╴<Token ':' at 105:106 (Delimiter.ModeChange)>
 │        ├╴<Token '*' at 106:107 (Text.Template)>
 │        ╰╴<Token '}' at 107:108 (Name.Variable)>
 ├╴<Context Bash.command at 109-125 (3 children)>
 │  ├╴<Token 'proto' at 109:114 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 114:115 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 115-125 (3 children)>
 │     ├╴<Token '${' at 115:117 (Name.Variable)>
 │     ├╴<Token 'uri' at 117:120 (Name.Variable)>
 │     ╰╴<Context Bash.parameter at 120-125 (4 children)>
 │        ├╴<Token '%%' at 120:122 (Delimiter.ModeChange)>
 │        ├╴<Token ':' at 122:123 (Delimiter.ModeChange)>
 │        ├╴<Token '*' at 123:124 (Text.Template)>
 │        ╰╴<Token '}' at 124:125 (Name.Variable)>
 ├╴<Context Bash.command at 126-147 (3 children)>
 │  ├╴<Token 'file' at 126:130 (Name.Variable.Definition)>
 │  ├╴<Token '=' at 130:131 (Delimiter.Operator.Assignment)>
 │  ╰╴<Context Bash.assignment at 131-147 (2 children)>
 │     ├╴<Token '"' at 131:132 (Literal.String.Start)>
 │     ╰╴<Context Bash.dqstring at 132-147 (5 children)>
 │        ├╴<Token 'file:' at 132:137 (Literal.String)>
 │        ├╴<Token '${' at 137:139 (Name.Variable)>
 │        ├╴<Token 'uri' at 139:142 (Name.Variable)>
 │        ├╴<Context Bash.parameter at 142-146 (4 children)>
 │        │  ├╴<Token '#' at 142:143 (Delimiter.ModeChange)>
 │        │  ├╴<Token '*' at 143:144 (Text.Template)>
 │        │  ├╴<Token ':' at 144:145 (Delimiter.ModeChange)>
 │        │  ╰╴<Token '}' at 145:146 (Name.Variable)>
 │        ╰╴<Token '"' at 146:147 (Literal.String.End)>
 ├╴<Context Bash.command at 149-174 (4 children)>
 │  ├╴<Token 'if' at 149:151 (Keyword)>
 │  ├╴<Token 'kde-config' at 152:162 (Name.Command)>
 │  ├╴<Context Bash.arguments at 163-172 (1 child)>
 │  │  ╰╴<Token '--version' at 163:172 (Name.Property)>
 │  ╰╴<Token '|' at 173:174 (Delimiter.Connection)>
 ├╴<Context Bash.command at 174-193 (2 children)>
 │  ├╴<Token 'grep' at 174:178 (Name.Command.Definition)>
 │  ╰╴<Context Bash.arguments at 179-193 (3 children)>
 │     ├╴<Token '-q' at 179:181 (Name.Property)>
 │     ├╴<Token "'" at 182:183 (Literal.String.Start)>
 │     ╰╴<Context Bash.sqstring at 183-193 (2 children)>
 │        ├╴<Token 'KDE: [23]' at 183:192 (Literal.String)>
 │        ╰╴<Token "'" at 192:193 (Literal.String.End)>
 ├╴<Context Bash.command at 194-198 (1 child)>
 │  ╰╴<Token 'then' at 194:198 (Keyword)>
 ├╴<Context Bash.command at 207-221 (2 children)>
 │  ├╴<Token '#' at 207:208 (Comment)>
 │  ╰╴<Context Bash.comment at 208-221 (1 child)>
 │     ╰╴<Token ' for KDE 3.x:' at 208:221 (Comment)>
 ├╴<Context Bash.command at 230-245 (2 children)>
 │  ├╴<Token 'let' at 230:233 (Name.Builtin)>
 │  ╰╴<Context Bash.let_expr at 234-245 (4 children)>
 │     ├╴<Token 'line' at 234:238 (Name.Variable.Definition)>
 │     ├╴<Token '=' at 238:239 (Delimiter.Operator.Assignment)>
 │     ├╴<Token '--' at 239:241 (Delimiter.Operator)>
 │     ╰╴<Token 'line' at 241:245 (Name.Variable)>
 ├╴<Context Bash.command at 246-250 (1 child)>
 │  ╰╴<Token 'else' at 246:250 (Keyword)>
 ├╴<Context Bash.command at 259-273 (2 children)>
 │  ├╴<Token '#' at 259:260 (Comment)>
 │  ╰╴<Context Bash.comment at 260-273 (1 child)>
 │     ╰╴<Token ' for KDE 4.x:' at 260:273 (Comment)>
 ├╴<Context Bash.command at 282-295 (2 children)>
 │  ├╴<Token 'let' at 282:285 (Name.Builtin)>
 │  ╰╴<Context Bash.let_expr at 286-295 (4 children)>
 │     ├╴<Token 'col' at 286:289 (Name.Variable.Definition)>
 │     ├╴<Token '=' at 289:290 (Delimiter.Operator.Assignment)>
 │     ├╴<Token '++' at 290:292 (Delimiter.Operator)>
 │     ╰╴<Token 'col' at 292:295 (Name.Variable)>
 ├╴<Context Bash.command at 296-298 (1 child)>
 │  ╰╴<Token 'fi' at 296:298 (Keyword)>
 ├╴<Context Bash.command at 300-350 (3 children)>
 │  ├╴<Token 'exec' at 300:304 (Name.Builtin)>
 │  ├╴<Token 'kate' at 305:309 (Name.Command)>
 │  ╰╴<Context Bash.arguments at 310-350 (7 children)>
 │     ├╴<Token '--use' at 310:315 (Name.Property)>
 │     ├╴<Token '--line' at 316:322 (Name.Property)>
 │     ├╴<Token '$line' at 323:328 (Name.Variable)>
 │     ├╴<Token '--column' at 329:337 (Name.Property)>
 │     ├╴<Token '$col' at 338:342 (Name.Variable)>
 │     ├╴<Token '"' at 343:344 (Literal.String.Start)>
 │     ╰╴<Context Bash.dqstring at 344-350 (2 children)>
 │        ├╴<Token '$file' at 344:349 (Name.Variable)>
 │        ╰╴<Token '"' at 349:350 (Literal.String.End)>
 ├╴<Token '#' at 352:353 (Comment)>
 ├╴<Context Bash.comment at 353-377 (1 child)>
 │  ╰╴<Token ' here is a here document' at 353:377 (Comment)>
 ├╴<Context Bash.command at 378-434 (2 children)>
 │  ├╴<Token 'cat' at 378:381 (Name.Command.Definition)>
 │  ╰╴<Context Bash.arguments at 382-434 (2 children)>
 │     ├╴<Token '<<' at 382:384 (Delimiter.Direction)>
 │     ╰╴<Context Bash.here_document* at 384-434 (9 children)>
 │        ├╴<Context Bash.command at 384-387 (1 child)>
 │        │  ╰╴<Context Bash.arguments at 384-387 (1 child)>
 │        │     ╰╴<Token 'EOF' at 384:387 (Text)>
 │        ├╴<Token '\nblabla blabla\nblabla ' at 387:409 (Literal.Verbatim)>
 │        ├╴<Token '$variable' at 409:418 (Name.Variable)>
 │        ├╴<Token ', ' at 418:420 (Literal.Verbatim)>
 │        ├╴<Token '$' at 420:421 (Name.Variable)>
 │        ├╴<Token '(' at 421:422 (Delimiter.Start)>
 │        ├╴<Context Bash.subshell at 422-430 (2 children)>
 │        │  ├╴<Context Bash.command* at 422-429 (1 child)>
 │        │  │  ╰╴<Token 'command' at 422:429 (Name.Command)>
 │        │  ╰╴<Token ')' at 429:430 (Delimiter.End)>
 │        ├╴<Token '\n' at 430:431 (Literal.Verbatim)>
 │        ╰╴<Token 'EOF' at 431:434 (Name.Identifier)>
 ├╴<Token '#' at 436:437 (Comment)>
 ├╴<Context Bash.comment at 437-466 (1 child)>
 │  ╰╴<Token ' and here is...ther, quoted.' at 437:466 (Comment)>
 ╰╴<Context Bash.command at 467-529 (2 children)>
    ├╴<Token 'cat' at 467:470 (Name.Command.Definition)>
    ╰╴<Context Bash.arguments at 471-529 (2 children)>
       ├╴<Token '<<-' at 471:474 (Delimiter.Direction)>
       ╰╴<Context Bash.here_document_quoted* at 474-529 (3 children)>
          ├╴<Context Bash.command at 474-479 (1 child)>
          │  ╰╴<Context Bash.arguments at 474-479 (2 children)>
          │     ├╴<Token '"' at 474:475 (Literal.String.Start)>
          │     ╰╴<Context Bash.dqstring at 475-479 (2 children)>
          │        ├╴<Token 'EOF' at 475:478 (Literal.String)>
          │        ╰╴<Token '"' at 478:479 (Literal.String.End)>
          ├╴<Token '\n\tblabla b... $(command)\n' at 479:525 (Literal.Verbatim)>
          ╰╴<Token 'EOF' at 526:529 (Name.Identifier)>