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

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