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

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