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

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