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

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