OILS / doc / ref / chap-special-var.md View on Github | oils.pub

510 lines, 295 significant
1---
2title: Special Variables (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 **Special Variables**
13
14</div>
15
16This chapter describes special variables for OSH and YSH.
17
18<span class="in-progress">(in progress)</span>
19
20<div id="dense-toc">
21</div>
22
23## YSH Vars
24
25### ARGV
26
27ARGV is List of strings contaning an argument array. This is the YSH
28replacement for shell's `"$@"`.
29
30- At the top level, it contains the arguments passed to the shell.
31- Inside an **open** [proc][], it contains the arguments passed to the proc.
32
33For example:
34
35 proc p { # open proc, ARGV is swithout signature
36 echo @ARGV
37 }
38 p a b c # => a b c
39
40In contrast, inside a closed proc, `ARGV` is empty.
41
42 proc p () { # closed proc
43 echo @ARGV
44 }
45
46[proc]: chap-ysh-cmd.html#proc
47
48### ENV
49
50An `Obj` that's populated with environment variables. Example usage:
51
52 var x = ENV.PYTHONPATH
53 echo $[ENV.SSH_AUTH_SOCK]
54 setglobal ENV.PYTHONPATH = '.'
55
56It's initialized exactly **once** per process, in any of these situations:
57
581. At shell startup, if `shopt --set env_obj` is on. This is true when invoking
59 `bin/ysh`.
602. When running `bin/osh -o ysh:upgrade` or `ysh:all`.
613. When running `shopt --set ysh:upgrade` or `ysh:all`.
62
63Related: [ysh-shopt][], [osh-usage][]
64
65[ysh-shopt]: chap-builtin-cmd.html#ysh-shopt
66[osh-usage]: chap-front-end.html#osh-usage
67
68---
69
70When launching an external command, the shell creates a Unix `environ` from the
71`ENV` Obj. This means that mutating it affects all subsequent processes:
72
73 setglobal ENV.PYTHONPATH = '.'
74 ./foo.py
75 ./bar.py
76
77You can also limit the change to a single process, without `ENV`:
78
79 PYTHONPATH=. ./foo.py
80 ./bar.py # unaffected
81
82---
83
84YSH reads these ENV variables:
85
86- `PATH` - where to look for executables
87- `PS1` - how to print the prompt
88- TODO: `PS4` - how to show execution traces
89- `YSH_HISTFILE` (`HISTFILE` in OSH) - where to read/write command history
90- `HOME` - for tilde substitution ([tilde-sub])
91
92[tilde-sub]: chap-word-lang.html#tilde-sub
93
94### `__defaults__`
95
96The shell puts some default settings in this `Dict`. In certain situations, it
97consults `__defaults__` after consulting `ENV`. For example:
98
99- if `ENV.PATH` is not set, consult `__defaults__.PATH`
100- if `ENV.PS1` is not set, consult `__defaults__.PS1`
101
102<!-- TODO: consider renaming to DEF.PS1 ? -->
103
104### `__builtins__`
105
106An object that contains names visible in every module.
107
108If a name is not visible in the local scope, or module global scope, then it's
109looked up in `__builtins__`.
110
111### `__sh_function__`
112
113A Dict that reflects the shell functions defined in the Oils interpreter.
114
115 = keys(__sh_function__) # enumerate all shell functions
116
117 = get(__sh_function__, 'f') # get a value of type Proc
118
119 = __sh_function__ => get('f') # synonym for the above
120
121You can reflect on [doc comments][doc-comment] by calling the [docComment][]
122method on a value of type [Proc][].
123
124[doc-comment]: chap-front-end.html#doc-comment
125[docComment]: chap-type-method.html#docComment
126[Proc]: chap-type-method.html#Proc
127
128### `_this_dir`
129
130The directory the current script resides in. This knows about 3 situations:
131
132- The location of `oshrc` in an interactive shell
133- The location of the main script, e.g. in `osh myscript.sh`
134- The location of script loaded with the `source` builtin
135
136It's useful for "relative imports".
137
138## YSH Status
139
140### `_status`
141
142DEPRECATED: Use `_error.code` instead.
143
144### `_error`
145
146A `Dict` that's set by the `try` builtin.
147
148The integer `_error.code` is always present:
149
150 try {
151 ls /tmp
152 }
153 echo "status is $[_error.code]"
154
155Some errors also have a `message` field, like JSON/J8 encoding/decoding errors,
156and user errors from the [error][] builtin.
157
158 try {
159 echo $[toJson( /d+/ )] # invalid Eggex type
160 }
161 echo "failed: $[_error.message]" # => failed: Can't serialize ...
162
163[error]: chap-builtin-cmd.html#error
164
165
166### `_pipeline_status`
167
168After a pipeline of processes is executed, this array contains the exit code of
169each process.
170
171Each exit code is an [Int](chap-type-method.html#Int). Compare with
172[`PIPESTATUS`](#PIPESTATUS).
173
174### `_process_sub_status`
175
176The exit status of all the process subs in the last command.
177
178## YSH Tracing
179
180### SHX_indent
181
182### SHX_punct
183
184### SHX_pid_str
185
186## YSH Read
187
188### _reply
189
190Builtins that `read` set this variable:
191
192 read --all < foo.txt
193 = _reply # => 'contents of file'
194
195 json read < foo.json
196 = _reply # => (Dict) {}
197
198## Oils VM
199
200### `OILS_VERSION`
201
202The version of Oils that's being run, e.g. `0.23.0`.
203
204<!-- TODO: specify comparison algorithm. -->
205
206### `LIB_OSH`
207
208The string `///osh`, which you can use with the [source][] builtin.
209
210 source $LIB_OSH/two.sh
211
212[source]: chap-builtin-cmd.html#source
213
214### `LIB_YSH`
215
216The string `///ysh`, which you can use with the [source][] builtin.
217
218 source $LIB_YSH/yblocks.ysh
219
220[source]: chap-builtin-cmd.html#source
221
222### `OILS_GC_THRESHOLD`
223
224At a GC point, if there are more than this number of live objects, collect
225garbage.
226
227### `OILS_GC_ON_EXIT`
228
229Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
230exits. By default, we let the OS clean up.
231
232Useful for ASAN testing.
233
234### `OILS_GC_STATS`
235
236When the shell process exists, print GC stats to stderr.
237
238### `OILS_GC_STATS_FD`
239
240When the shell process exists, print GC stats to this file descriptor.
241
242### `OILS_LOCALE_OK`
243
244Suppress the warning about `libc` locales that are not UTF-8.
245
246## libc locale
247
248### osh-locale
249
250At startup, reads these env vars and sets global vars in `libc`:
251
252- `LC_CTYPE`
253 - Affects how certain `libc` string functions handle bytes versus code
254 points. For example, what do `?` in globs and `.` in regexes mean?
255- `LC_COLLATE`
256 - Affects [glob][osh-glob] sort order.
257
258This is done with libc's `setlocale()` function.
259
260[osh-glob]: chap-word-lang.html#osh-glob
261
262### ysh-locale
263
264At startup, reads these env vars and sets global vars in `libc`:
265
266- `LC_CTYPE`
267
268This is done with libc's `setlocale()` function.
269
270[ysh-glob]: chap-word-lang.html#ysh-glob
271
272## Interactive
273
274### OILS_COMP_UI
275
276Set which completion UI to use. Defaults to `minimal`.
277
278- `minimal` - a UI that approximates the default behavior of GNU readline.
279- `nice` - a UI with a fancy pager and a prompt with horizontal scrolling instead of wrapping.
280
281This variable is currently only checked once during shell initialization.
282
283### HISTFILE
284
285Override the default OSH history location.
286
287### YSH_HISTFILE
288
289Override the default YSH history location.
290
291## Float
292
293### NAN
294
295The float value for "not a number".
296
297(The name is consistent with the C language.)
298
299### INFINITY
300
301The float value for "infinity". You can negate it to get "negative infinity".
302
303(The name is consistent with the C language.)
304
305## Module
306
307### `__provide__`
308
309A module is evaluated upon `use`. After evaluation, the names in the
310`__provide__` `List` are put in the resulting module `Obj` instance.
311
312<!--
313`__provide__` may also be a string, where 'p' stands for --procs, and 'f' stands for funcs.
314
315Or we could make it [1, 2] insetad
316-->
317
318## POSIX Special
319
320 $@ $* all arguments
321 $0 ... $9 ${10} each argument
322 $# the number of arguments
323 $? last exit status
324 $-
325 $$ PID of shell (does not change when shell forks)
326 $! ID of last job started, with & or bg
327
328Details on `$!`:
329
330- It's either
331 - the PID of a process
332 - the PID of the *last* part of the pipeline. (In contrast, the PGID of the
333 pipeline is the PID of the first part.)
334- The [`wait`](chap-builtin-cmd.html#wait) builtin knows how to find a
335 background pipeline job, given this ID.
336- It's set by:
337 - [`ampersand &`](chap-cmd-lang.html#ampersand)
338 - [`bg`](chap-builtin-cmd.html#bg)
339 - In particular, it's not set after Ctrl-Z, only after `bg`.
340
341## Shell Vars
342
343### IFS
344
345Used for word splitting. And the builtin `shSplit()` function.
346
347### LANG
348
349TODO: bash compat
350
351### GLOBIGNORE
352
353TODO: bash compat
354
355## Shell Options
356
357### SHELLOPTS
358
359bash compat: serialized options for the `set` builtin.
360
361### BASHOPTS
362
363bash compat: serialized options for the `shopt` builtin.
364
365(Not implemented.)
366
367## Other Env
368
369### HOME
370
371The `$HOME` env var is read by the shell, for:
372
3731. `~` expansion
3742. `~` abbreviation in the UI (the dirs builtin, `\W` in `$PS1`).
375
376The shell does not set $HOME. According to POSIX, the program that invokes the
377login shell should set it, based on `/etc/passwd`.
378
379### PATH
380
381A colon-separated string that's used to find executables to run.
382
383In YSH, it's `ENV.PATH`.
384
385## Other Special
386
387### BASH_REMATCH
388
389Result of regex evaluation `[[ $x =~ $pat ]]`.
390
391### PIPESTATUS
392
393After a pipeline of processes is executed, this array contains the exit code of
394each process.
395
396Each exit code is a [Str](chap-type-method.html#Str). Compare with
397[`_pipeline_status`](#_pipeline_status).
398
399## Platform
400
401### HOSTNAME
402
403The name of the "host" or machine that Oils is running on, determined by
404`gethostname()`.
405
406### OSTYPE
407
408The operating system that Oils is running on, determined by `uname()`.
409
410Examples: `linux darwin ...`
411
412## Call Stack
413
414### BASH_SOURCE
415
416### FUNCNAME
417
418### BASH_LINENO
419
420## Tracing
421
422### LINENO
423
424## Process State
425
426### BASHPID
427
428TODO
429
430### PPID
431
432TODO
433
434### UID
435
436### EUID
437
438## Process Stack
439
440## Shell State
441
442## Completion
443
444### COMP_WORDS
445
446An array of words, split by : and = for compatibility with bash. New
447completion scripts should use COMP_ARGV instead.
448
449### COMP_CWORD
450
451Discouraged; for compatibility with bash.
452
453### COMP_LINE
454
455Discouraged; for compatibility with bash.
456
457### COMP_POINT
458
459Discouraged; for compatibility with bash.
460
461### COMP_WORDBREAKS
462
463Discouraged; for compatibility with bash.
464
465### COMPREPLY
466
467User-defined completion functions should Fill this array with candidates. It
468is cleared on every completion request.
469
470### COMP_ARGV
471
472An array of partial command arguments to complete. Preferred over COMP_WORDS.
473The compadjust builtin uses this variable.
474
475(An OSH extension to bash.)
476
477## cd
478
479### PWD
480
481### OLDPWD
482
483### CDPATH
484
485## getopts
486
487### OPTIND
488
489### OPTARG
490
491### OPTERR
492
493## read
494
495### REPLY
496
497OSH read sets this:
498
499 read < myfile
500
501## Functions
502
503### RANDOM
504
505bash compat
506
507### SECONDS
508
509bash compat
510