1 | ---
|
2 | title: Mini Languages (Oils Reference)
|
3 | all_docs_url: ..
|
4 | body_css_class: width40
|
5 | default_highlighter: oils-sh
|
6 | preserve_anchor_case: yes
|
7 | ---
|
8 |
|
9 | <div class="doc-ref-header">
|
10 |
|
11 | [Oils Reference](index.html) —
|
12 | Chapter **Mini Languages**
|
13 |
|
14 | </div>
|
15 |
|
16 | This chapter describes "mini-languages" like glob patterns and brace expansion.
|
17 |
|
18 | In 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 |
|
32 | Arithmetic expressions are parsed and evaluated in many parts of POSIX shell
|
33 | and bash.
|
34 |
|
35 | Static:
|
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 |
|
46 | Dynamic:
|
47 |
|
48 | [[ 5 -eq 3+x ]] # but not test 5 -eq 3+x
|
49 |
|
50 | Array 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 |
|
62 | bash 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 |
|
70 | But 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 |
|
88 | Boolean expressions can be use the `test` builtin:
|
89 |
|
90 | test ! $x -a $y -o $z
|
91 |
|
92 | Or the `[[` command language:
|
93 |
|
94 | [[ ! $x && $y || $z ]]
|
95 |
|
96 | ### bool-infix
|
97 |
|
98 | Examples:
|
99 |
|
100 | test $a -nt $b
|
101 | test $x == $y
|
102 |
|
103 | ### bool-path
|
104 |
|
105 | Example:
|
106 |
|
107 | test -d /etc
|
108 | test -e /
|
109 | test -f myfile
|
110 |
|
111 | YSH 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 |
|
128 | Note: `name[index]` doesn't implement arithmetic expressions / dynamic parsing,
|
129 | as in bash.
|
130 |
|
131 | ## Patterns
|
132 |
|
133 | ### glob-pat
|
134 |
|
135 | TODO: glob syntax
|
136 |
|
137 | ### extglob
|
138 |
|
139 | TODO: extended glob syntax
|
140 |
|
141 | ### regex
|
142 |
|
143 | Part of [dbracket](chap-cmd-lang.html#dbracket)
|
144 |
|
145 | ## Other Sublang
|
146 |
|
147 | ### braces
|
148 |
|
149 | ### histsub
|
150 |
|
151 | History substitution uses `!`.
|
152 |
|
153 | ### char-escapes
|
154 |
|
155 | These backslash escape sequences are used in [echo
|
156 | -e](chap-builtin-cmd.html#echo), [printf](chap-builtin-cmd.html#printf), and in
|
157 | C-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 |
|
173 | Also:
|
174 |
|
175 | \" Double quote.
|
176 |
|
177 | Inconsistent octal escapes:
|
178 |
|
179 | \0NNN echo -e '\0123'
|
180 | \NNN printf '\123'
|
181 | echo $'\123'
|
182 |
|
183 | TODO: Verify other differences between `echo -e`, `printf`, and `$''`. See
|
184 | `frontend/lexer_def.py`.
|
185 |
|