OILS / doc / ref / chap-mini-lang.md View on Github | oilshell.org

185 lines, 114 significant
1---
2title: Mini Languages (Oils Reference)
3all_docs_url: ..
4body_css_class: width40
5default_highlighter: oils-sh
6preserve_anchor_case: yes
7---
8
9<div class="doc-ref-header">
10
11[Oils Reference](index.html) &mdash;
12Chapter **Mini Languages**
13
14</div>
15
16This chapter describes "mini-languages" like glob patterns and brace expansion.
17
18In contrast, the main sub languages of YSH are [command](chap-cmd-lang.html),
19[word](chap-word-lang.html), and [expression](chap-expr-lang.html).
20
21<span class="in-progress">(in progress)</span>
22
23<div id="dense-toc">
24</div>
25
26<h2 id="sublang">Other Shell Sublanguages</h2>
27
28## Arithmetic
29
30### arith-context
31
32Arithmetic expressions are parsed and evaluated in many parts of POSIX shell
33and bash.
34
35Static:
36
37 a=$(( x + 1 )) # POSIX shell
38
39 # bash
40 (( a = x + 1 ))
41
42 for (( i = 0; i < n; ++i )); do
43 echo $i
44 done
45
46Dynamic:
47
48 [[ 5 -eq 3+x ]] # but not test 5 -eq 3+x
49
50Array index contexts:
51
52 echo ${a[i+1]} # get
53 echo ${#a[i+1]} # calculate
54
55 a[i+1]=foo # set
56
57 printf -v 'a[i+1]' # assign to this location
58 unset 'a[i+1]' # unset location
59
60 echo ${a[@] : i+1 : i+2 } # bash slicing
61
62bash allows similar array expressions with `test -v`:
63
64 test -v 'array[i+1]' # is array item set?
65 [[ -v 'array[i+1]' ]] # ditto
66
67 test -v 'assoc[$key]' # is assoc array key set?
68 [[ -v 'assoc[$key]' ]] # ditto
69
70But OSH allows only integers and "bare" string constants:
71
72 test -v 'array[42]' # is array item set?
73
74 test -v 'assoc[key]' # is assoc array key set?
75
76### sh-numbers
77
78### sh-arith
79
80### sh-logical
81
82### sh-bitwise
83
84## Boolean
85
86### bool-expr
87
88Boolean expressions can be use the `test` builtin:
89
90 test ! $x -a $y -o $z
91
92Or the `[[` command language:
93
94 [[ ! $x && $y || $z ]]
95
96### bool-infix
97
98Examples:
99
100 test $a -nt $b
101 test $x == $y
102
103### bool-path
104
105Example:
106
107 test -d /etc
108 test -e /
109 test -f myfile
110
111YSH has long flags:
112
113 test --dir /etc
114 test --exists /
115 test --file myfile
116
117### bool-str
118
119 test -n foo # => status 0 / true -- foo is non-empty
120 test -z '' # => status 0 / true -- '' is empty / zero-length
121
122### bool-other
123
124 test -o errexit # is the option set?
125 test -v var_name # is variable defined?
126 test -v name[index] # is an entry in a container set?
127
128Note: `name[index]` doesn't implement arithmetic expressions / dynamic parsing,
129as in bash.
130
131## Patterns
132
133### glob-pat
134
135TODO: glob syntax
136
137### extglob
138
139TODO: extended glob syntax
140
141### regex
142
143Part of [dbracket](chap-cmd-lang.html#dbracket)
144
145## Other Sublang
146
147### braces
148
149### histsub
150
151History substitution uses `!`.
152
153### char-escapes
154
155These backslash escape sequences are used in [echo
156-e](chap-builtin-cmd.html#echo), [printf](chap-builtin-cmd.html#printf), and in
157C-style strings like `$'foo\n'`:
158
159 \\ backslash
160 \a alert (BEL)
161 \b backspace
162 \c stop processing remaining input
163 \e the escape character \x1b
164 \f form feed
165 \n newline
166 \r carriage return
167 \t tab
168 \v vertical tab
169 \xHH the byte with value HH, in hexadecimal
170 \uHHHH the unicode char with value HHHH, in hexadecimal
171 \UHHHHHHHH the unicode char with value HHHHHHHH, in hexadecimal
172
173Also:
174
175 \" Double quote.
176
177Inconsistent octal escapes:
178
179 \0NNN echo -e '\0123'
180 \NNN printf '\123'
181 echo $'\123'
182
183TODO: Verify other differences between `echo -e`, `printf`, and `$''`. See
184`frontend/lexer_def.py`.
185