| 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 | test -v 'assoc[$myvar]' # is assoc array key set?
|
| 66 |
|
| 67 | [[ -v 'array[i+1]' ]] # ditto
|
| 68 | [[ -v 'assoc[$myvar]' ]]
|
| 69 |
|
| 70 | But 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 |
|
| 87 | Boolean expressions can be use the `test` builtin:
|
| 88 |
|
| 89 | test ! $x -a $y -o $z
|
| 90 |
|
| 91 | Or the `[[` command language:
|
| 92 |
|
| 93 | [[ ! $x && $y || $z ]]
|
| 94 |
|
| 95 | ### bool-infix
|
| 96 |
|
| 97 | Examples:
|
| 98 |
|
| 99 | test $a -nt $b
|
| 100 | test $x == $y
|
| 101 |
|
| 102 | ### bool-path
|
| 103 |
|
| 104 | Example:
|
| 105 |
|
| 106 | test -d /etc
|
| 107 | test -e /
|
| 108 | test -f myfile
|
| 109 |
|
| 110 | YSH 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 if a shell option is set:
|
| 124 |
|
| 125 | test -o errexit
|
| 126 |
|
| 127 | Test the values of variables:
|
| 128 |
|
| 129 | test -v var_name # is variable defined?
|
| 130 | test -v name[index] # is an entry in a container set?
|
| 131 |
|
| 132 | Notes:
|
| 133 |
|
| 134 | - In `name[index]`, OSH doesn't allow arithmetic expressions / dynamic parsing,
|
| 135 | as bash does.
|
| 136 | - `shopt --set strict_word_eval` exposes "syntax errors" in `name[index]`, and
|
| 137 | is recommended.
|
| 138 | - Without this option, `test -v` will silently return `1` (false) when given
|
| 139 | nonsense input, like `test -v /`.
|
| 140 |
|
| 141 | ## Patterns
|
| 142 |
|
| 143 | ### glob-pat
|
| 144 |
|
| 145 | Glob patterns look like:
|
| 146 |
|
| 147 | echo *.py # Ends with .py
|
| 148 | echo *.[ch] # Ends with .c or .h
|
| 149 |
|
| 150 | This syntax is used in:
|
| 151 |
|
| 152 | - "Array of words" contexts
|
| 153 | - [simple-command][] - like `echo *.py`
|
| 154 | - bash arrays `a=( *.py )`
|
| 155 | - YSH arrays `var a = :| *.py |`
|
| 156 | - for loops `for x in *.py; do ...`
|
| 157 | - [case][] patterns
|
| 158 | - [dbracket][] - `[[ x == *.py ]]`
|
| 159 | - Word operations
|
| 160 | - [op-strip][] - `${x#*.py}`
|
| 161 | - [op-patsub][] - `${x//*.py/replace}` -
|
| 162 |
|
| 163 | [simple-command]: chap-cmd-lang.html#simple-command
|
| 164 | [case]: chap-cmd-lang.html#case
|
| 165 | [dbracket]: chap-cmd-lang.html#dbracket
|
| 166 |
|
| 167 | [op-strip]: chap-word-lang.html#op-strip
|
| 168 | [op-patsub]: chap-word-lang.html#op-patsub
|
| 169 |
|
| 170 | ### extglob
|
| 171 |
|
| 172 | Extended globs let you use logical operations with globs.
|
| 173 |
|
| 174 | They may be **slow**. Regexes and eggexes are preferred.
|
| 175 |
|
| 176 | echo @(*.cc|*.h) # Show files ending with .cc or .h
|
| 177 | echo !(*.cc|*.h) # Show every file that does NOT end with .cc or .h
|
| 178 |
|
| 179 | Extended globs can appear in most of the places globs can, except
|
| 180 | [op-patsub][] (because we implement it by translating.
|
| 181 |
|
| 182 | ### regex
|
| 183 |
|
| 184 | POSIX ERE (extended regular expressions) are part of bash's [dbracket][]:
|
| 185 |
|
| 186 | x=123
|
| 187 | if [[ x =~ '[0-9]+ ]]; then
|
| 188 | echo 'looks like a number'
|
| 189 | fi
|
| 190 |
|
| 191 | ## Other Sublang
|
| 192 |
|
| 193 | ### braces
|
| 194 |
|
| 195 | Brace expansion saves you typing:
|
| 196 |
|
| 197 | $ echo {foo,bar}@example.com
|
| 198 | foo@example.com bar@example.com
|
| 199 |
|
| 200 | You can use it with number ranges:
|
| 201 |
|
| 202 | $ echo foo{1..3}
|
| 203 | foo1 foo2 foo3
|
| 204 |
|
| 205 | (The numbers must be **constant**.)
|
| 206 |
|
| 207 | Technically, it does a cartesian product, which is 3 X 2 in this case:
|
| 208 |
|
| 209 | $ for x in foo{1..3}-{X,Y}; do echo $x; done
|
| 210 | foo1-X
|
| 211 | foo1-Y
|
| 212 | foo2-X
|
| 213 | foo2-Y
|
| 214 | foo3-X
|
| 215 | foo3-Y
|
| 216 |
|
| 217 | ### histsub
|
| 218 |
|
| 219 | History substitution uses `!`.
|
| 220 |
|
| 221 | ### char-escapes
|
| 222 |
|
| 223 | These backslash escape sequences are used in [echo
|
| 224 | -e](chap-builtin-cmd.html#echo), [printf](chap-builtin-cmd.html#printf), and in
|
| 225 | C-style strings like `$'foo\n'`:
|
| 226 |
|
| 227 | \\ backslash
|
| 228 | \a alert (BEL)
|
| 229 | \b backspace
|
| 230 | \c stop processing remaining input
|
| 231 | \e the escape character \x1b
|
| 232 | \f form feed
|
| 233 | \n newline
|
| 234 | \r carriage return
|
| 235 | \t tab
|
| 236 | \v vertical tab
|
| 237 | \xHH the byte with value HH, in hexadecimal
|
| 238 | \uHHHH the unicode char with value HHHH, in hexadecimal
|
| 239 | \UHHHHHHHH the unicode char with value HHHHHHHH, in hexadecimal
|
| 240 |
|
| 241 | Also:
|
| 242 |
|
| 243 | \" Double quote.
|
| 244 |
|
| 245 | Inconsistent octal escapes:
|
| 246 |
|
| 247 | \0NNN echo -e '\0123'
|
| 248 | \NNN printf '\123'
|
| 249 | echo $'\123'
|
| 250 |
|
| 251 | TODO: Verify other differences between `echo -e`, `printf`, and `$''`. See
|
| 252 | `frontend/lexer_def.py`.
|
| 253 |
|