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

184 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 test -v 'assoc[$myvar]' # is assoc array key set?
66
67 [[ -v 'array[i+1]' ]] # ditto
68 [[ -v 'assoc[$myvar]' ]]
69
70But OSH allows only integers and "bare" string constants:
71
72 test -v 'array[42]' # is array item set?
73 test -v 'assoc[key]' # is assoc array key set?
74
75### sh-numbers
76
77### sh-arith
78
79### sh-logical
80
81### sh-bitwise
82
83## Boolean
84
85### bool-expr
86
87Boolean expressions can be use the `test` builtin:
88
89 test ! $x -a $y -o $z
90
91Or the `[[` command language:
92
93 [[ ! $x && $y || $z ]]
94
95### bool-infix
96
97Examples:
98
99 test $a -nt $b
100 test $x == $y
101
102### bool-path
103
104Example:
105
106 test -d /etc
107 test -e /
108 test -f myfile
109
110YSH has long flags:
111
112 test --dir /etc
113 test --exists /
114 test --file myfile
115
116### bool-str
117
118 test -n foo # => status 0 / true -- foo is non-empty
119 test -z '' # => status 0 / true -- '' is empty / zero-length
120
121### bool-other
122
123 test -o errexit # is the option set?
124 test -v var_name # is variable defined?
125 test -v name[index] # is an entry in a container set?
126
127Note: `name[index]` doesn't implement arithmetic expressions / dynamic parsing,
128as in bash.
129
130## Patterns
131
132### glob-pat
133
134TODO: glob syntax
135
136### extglob
137
138TODO: extended glob syntax
139
140### regex
141
142Part of [dbracket](chap-cmd-lang.html#dbracket)
143
144## Other Sublang
145
146### braces
147
148### histsub
149
150History substitution uses `!`.
151
152### char-escapes
153
154These backslash escape sequences are used in [echo
155-e](chap-builtin-cmd.html#echo), [printf](chap-builtin-cmd.html#printf), and in
156C-style strings like `$'foo\n'`:
157
158 \\ backslash
159 \a alert (BEL)
160 \b backspace
161 \c stop processing remaining input
162 \e the escape character \x1b
163 \f form feed
164 \n newline
165 \r carriage return
166 \t tab
167 \v vertical tab
168 \xHH the byte with value HH, in hexadecimal
169 \uHHHH the unicode char with value HHHH, in hexadecimal
170 \UHHHHHHHH the unicode char with value HHHHHHHH, in hexadecimal
171
172Also:
173
174 \" Double quote.
175
176Inconsistent octal escapes:
177
178 \0NNN echo -e '\0123'
179 \NNN printf '\123'
180 echo $'\123'
181
182TODO: Verify other differences between `echo -e`, `printf`, and `$''`. See
183`frontend/lexer_def.py`.
184