OILS / doc / ref / chap-special-var.md View on Github | oilshell.org

415 lines, 232 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
27Replacement for `"$@"`
28
29### ENV
30
31An `Obj` that's populated with environment variables. Example usage:
32
33 var x = ENV.PYTHONPATH
34 echo $[ENV.SSH_AUTH_SOCK]
35
36It's initialized exactly **once** per process, in any of these situations:
37
381. At shell startup, if `shopt --set env_obj` is on. This is true when invoking
39 `bin/ysh`.
402. When running `bin/osh -o ysh:upgrade` or `ysh:all`.
413. When running `shopt --set ysh:upgrade` or `ysh:all`.
42
43Related: [ysh-shopt][], [osh-usage][]
44
45[ysh-shopt]: chap-builtin-cmd.html#ysh-shopt
46[osh-usage]: chap-front-end.html#osh-usage
47
48---
49
50When launching an external command, the shell creates a Unix `environ` from the
51`ENV` Obj. This means that mutating it affects all subsequent processes:
52
53 setglobal ENV.PYTHONPATH = '.'
54 ./foo.py
55 ./bar.py
56
57You can also limit the change to a single process, without `ENV`:
58
59 PYTHONPATH=. ./foo.py
60 ./bar.py # unaffected
61
62---
63
64YSH reads these ENV variables:
65
66- `PATH` - where to look for executables
67- `PS1` - how to print the prompt
68- TODO: `PS4` - how to show execution traces
69- `YSH_HISTFILE` (`HISTFILE` in OSH) - where to read/write command history
70- `HOME` - for tilde substitution ([tilde-sub])
71
72[tilde-sub]: chap-word-lang.html#tilde-sub
73
74### `__defaults__`
75
76The shell puts some default settings in this `Dict`. In certain situations, it
77consults `__defaults__` after consulting `ENV`. For example:
78
79- if `ENV.PATH` is not set, consult `__defaults__.PATH`
80- if `ENV.PS1` is not set, consult `__defaults__.PS1`
81
82<!-- TODO: consider renaming to DEF.PS1 ? -->
83
84### `__builtins__`
85
86An object that contains names visible in every module.
87
88If a name is not visible in the local scope, or module global scope, then it's
89looked up in `__builtins__`.
90
91### `_this_dir`
92
93The directory the current script resides in. This knows about 3 situations:
94
95- The location of `oshrc` in an interactive shell
96- The location of the main script, e.g. in `osh myscript.sh`
97- The location of script loaded with the `source` builtin
98
99It's useful for "relative imports".
100
101## YSH Status
102
103### `_status`
104
105DEPRECATED: Use `_error.code` instead.
106
107### `_error`
108
109A `Dict` that's set by the `try` builtin.
110
111The integer `_error.code` is always present:
112
113 try {
114 ls /tmp
115 }
116 echo "status is $[_error.code]"
117
118Some errors also have a `message` field, like JSON/J8 encoding/decoding errors,
119and user errors from the [error][] builtin.
120
121 try {
122 echo $[toJson( /d+/ )] # invalid Eggex type
123 }
124 echo "failed: $[_error.message]" # => failed: Can't serialize ...
125
126[error]: chap-builtin-cmd.html#error
127
128
129### `_pipeline_status`
130
131After a pipeline of processes is executed, this array contains the exit code of
132each process.
133
134Each exit code is an [Int](chap-type-method.html#Int). Compare with
135[`PIPESTATUS`](#PIPESTATUS).
136
137### `_process_sub_status`
138
139The exit status of all the process subs in the last command.
140
141## YSH Tracing
142
143### SHX_indent
144
145### SHX_punct
146
147### SHX_pid_str
148
149## YSH Read
150
151### _reply
152
153Builtins that `read` set this variable:
154
155 read --all < foo.txt
156 = _reply # => 'contents of file'
157
158 json read < foo.json
159 = _reply # => (Dict) {}
160
161## Oils VM
162
163### `OILS_VERSION`
164
165The version of Oils that's being run, e.g. `0.23.0`.
166
167<!-- TODO: specify comparison algorithm. -->
168
169### `LIB_OSH`
170
171The string `///osh`, which you can use with the [source][] builtin.
172
173 source $LIB_OSH/two.sh
174
175[source]: chap-builtin-cmd.html#source
176
177### `LIB_YSH`
178
179The string `///ysh`, which you can use with the [source][] builtin.
180
181 source $LIB_YSH/yblocks.ysh
182
183[source]: chap-builtin-cmd.html#source
184
185### `OILS_GC_THRESHOLD`
186
187At a GC point, if there are more than this number of live objects, collect
188garbage.
189
190### `OILS_GC_ON_EXIT`
191
192Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
193exits. By default, we let the OS clean up.
194
195Useful for ASAN testing.
196
197### `OILS_GC_STATS`
198
199When the shell process exists, print GC stats to stderr.
200
201### `OILS_GC_STATS_FD`
202
203When the shell process exists, print GC stats to this file descriptor.
204
205## Float
206
207### NAN
208
209The float value for "not a number".
210
211(The name is consistent with the C language.)
212
213### INFINITY
214
215The float value for "infinity". You can negate it to get "negative infinity".
216
217(The name is consistent with the C language.)
218
219## Module
220
221### `__provide__`
222
223A module is evaluated upon `use`. After evaluation, the names in the
224`__provide__` `List` are put in the resulting module `Obj` instance.
225
226<!--
227`__provide__` may also be a string, where 'p' stands for --procs, and 'f' stands for funcs.
228
229Or we could make it [1, 2] insetad
230-->
231
232## POSIX Special
233
234`$@ $* $# $? $- $$ $! $0 $9`
235
236## Shell Vars
237
238### IFS
239
240Used for word splitting. And the builtin `shSplit()` function.
241
242### LANG
243
244TODO: bash compat
245
246### GLOBIGNORE
247
248TODO: bash compat
249
250## Shell Options
251
252### SHELLOPTS
253
254bash compat: serialized options for the `set` builtin.
255
256### BASHOPTS
257
258bash compat: serialized options for the `shopt` builtin.
259
260(Not implemented.)
261
262## Other Env
263
264### HOME
265
266The `$HOME` env var is read by the shell, for:
267
2681. `~` expansion
2692. `~` abbreviation in the UI (the dirs builtin, `\W` in `$PS1`).
270
271The shell does not set $HOME. According to POSIX, the program that invokes the
272login shell should set it, based on `/etc/passwd`.
273
274### PATH
275
276A colon-separated string that's used to find executables to run.
277
278In YSH, it's `ENV.PATH`.
279
280## Other Special
281
282### BASH_REMATCH
283
284Result of regex evaluation `[[ $x =~ $pat ]]`.
285
286### PIPESTATUS
287
288After a pipeline of processes is executed, this array contains the exit code of
289each process.
290
291Each exit code is a [Str](chap-type-method.html#Str). Compare with
292[`_pipeline_status`](#_pipeline_status).
293
294## Platform
295
296### HOSTNAME
297
298The name of the "host" or machine that Oils is running on, determined by
299`gethostname()`.
300
301### OSTYPE
302
303The operating system that Oils is running on, determined by `uname()`.
304
305Examples: `linux darwin ...`
306
307## Call Stack
308
309### BASH_SOURCE
310
311### FUNCNAME
312
313### BASH_LINENO
314
315## Tracing
316
317### LINENO
318
319## Process State
320
321### BASHPID
322
323TODO
324
325### PPID
326
327TODO
328
329### UID
330
331### EUID
332
333## Process Stack
334
335## Shell State
336
337## Completion
338
339### COMP_WORDS
340
341An array of words, split by : and = for compatibility with bash. New
342completion scripts should use COMP_ARGV instead.
343
344### COMP_CWORD
345
346Discouraged; for compatibility with bash.
347
348### COMP_LINE
349
350Discouraged; for compatibility with bash.
351
352### COMP_POINT
353
354Discouraged; for compatibility with bash.
355
356### COMP_WORDBREAKS
357
358Discouraged; for compatibility with bash.
359
360### COMPREPLY
361
362User-defined completion functions should Fill this array with candidates. It
363is cleared on every completion request.
364
365### COMP_ARGV
366
367An array of partial command arguments to complete. Preferred over COMP_WORDS.
368The compadjust builtin uses this variable.
369
370(An OSH extension to bash.)
371
372## History
373
374### HISTFILE
375
376Override the default OSH history location.
377
378### YSH_HISTFILE
379
380Override the default YSH history location.
381
382## cd
383
384### PWD
385
386### OLDPWD
387
388### CDPATH
389
390## getopts
391
392### OPTIND
393
394### OPTARG
395
396### OPTERR
397
398## read
399
400### REPLY
401
402OSH read sets this:
403
404 read < myfile
405
406## Functions
407
408### RANDOM
409
410bash compat
411
412### SECONDS
413
414bash compat
415