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

364 lines, 200 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
214$HOME is used for:
215
2161. ~ expansion
2172. ~ abbreviation in the UI (the dirs builtin, \W in $PS1).
218
219Note: The shell doesn't set $HOME. According to POSIX, the program that
220invokes the login shell sets it based on /etc/passwd.
221
222### PATH
223
224A colon-separated string that's used to find executables to run.
225
226
227## POSIX Special
228
229## Other Special
230
231### BASH_REMATCH
232
233Result of regex evaluation `[[ $x =~ $pat ]]`.
234
235### PIPESTATUS
236
237After a pipeline of processes is executed, this array contains the exit code of
238each process.
239
240Each exit code is a [Str](chap-type-method.html#Str). Compare with
241[`_pipeline_status`](#_pipeline_status).
242
243## Platform
244
245### HOSTNAME
246
247The name of the "host" or machine that Oils is running on, determined by
248`gethostname()`.
249
250### OSTYPE
251
252The operating system that Oils is running on, determined by `uname()`.
253
254Examples: `linux darwin ...`
255
256## Call Stack
257
258### BASH_SOURCE
259
260### FUNCNAME
261
262### BASH_LINENO
263
264## Tracing
265
266### LINENO
267
268## Process State
269
270### BASHPID
271
272TODO
273
274### PPID
275
276TODO
277
278### UID
279
280### EUID
281
282## Process Stack
283
284## Shell State
285
286## Completion
287
288### COMP_WORDS
289
290An array of words, split by : and = for compatibility with bash. New
291completion scripts should use COMP_ARGV instead.
292
293### COMP_CWORD
294
295Discouraged; for compatibility with bash.
296
297### COMP_LINE
298
299Discouraged; for compatibility with bash.
300
301### COMP_POINT
302
303Discouraged; for compatibility with bash.
304
305### COMP_WORDBREAKS
306
307Discouraged; for compatibility with bash.
308
309### COMPREPLY
310
311User-defined completion functions should Fill this array with candidates. It
312is cleared on every completion request.
313
314### COMP_ARGV
315
316An array of partial command arguments to complete. Preferred over COMP_WORDS.
317The compadjust builtin uses this variable.
318
319(An OSH extension to bash.)
320
321## History
322
323### HISTFILE
324
325Override the default OSH history location.
326
327### YSH_HISTFILE
328
329Override the default YSH history location.
330
331## cd
332
333### PWD
334
335### OLDPWD
336
337### CDPATH
338
339## getopts
340
341### OPTIND
342
343### OPTARG
344
345### OPTERR
346
347## read
348
349### REPLY
350
351OSH read sets this:
352
353 read < myfile
354
355## Functions
356
357### RANDOM
358
359bash compat
360
361### SECONDS
362
363bash compat
364