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

306 lines, 167 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.9.0`.
105
106<!-- TODO: specify comparison algorithm. -->
107
108### `OILS_GC_THRESHOLD`
109
110At a GC point, if there are more than this number of live objects, collect
111garbage.
112
113### `OILS_GC_ON_EXIT`
114
115Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
116exits. By default, we let the OS clean up.
117
118Useful for ASAN testing.
119
120### `OILS_GC_STATS`
121
122When the shell process exists, print GC stats to stderr.
123
124### `OILS_GC_STATS_FD`
125
126When the shell process exists, print GC stats to this file descriptor.
127
128## Shell Vars
129
130### IFS
131
132Used for word splitting. And the builtin `shSplit()` function.
133
134### LANG
135
136TODO: bash compat
137
138### GLOBIGNORE
139
140TODO: bash compat
141
142## Shell Options
143
144### SHELLOPTS
145
146bash compat: serialized options for the `set` builtin.
147
148### BASHOPTS
149
150bash compat: serialized options for the `shopt` builtin.
151
152## Other Env
153
154### HOME
155
156$HOME is used for:
157
1581. ~ expansion
1592. ~ abbreviation in the UI (the dirs builtin, \W in $PS1).
160
161Note: The shell doesn't set $HOME. According to POSIX, the program that
162invokes the login shell sets it based on /etc/passwd.
163
164### PATH
165
166A colon-separated string that's used to find executables to run.
167
168
169## POSIX Special
170
171## Other Special
172
173### BASH_REMATCH
174
175Result of regex evaluation `[[ $x =~ $pat ]]`.
176
177### PIPESTATUS
178
179After a pipeline of processes is executed, this array contains the exit code of
180each process.
181
182Each exit code is a [Str](chap-type-method.html#Str). Compare with
183[`_pipeline_status`](#_pipeline_status).
184
185## Platform
186
187### HOSTNAME
188
189The name of the "host" or machine that Oils is running on, determined by
190`gethostname()`.
191
192### OSTYPE
193
194The operating system that Oils is running on, determined by `uname()`.
195
196Examples: `linux darwin ...`
197
198## Call Stack
199
200### BASH_SOURCE
201
202### FUNCNAME
203
204### BASH_LINENO
205
206## Tracing
207
208### LINENO
209
210## Process State
211
212### BASHPID
213
214TODO
215
216### PPID
217
218TODO
219
220### UID
221
222### EUID
223
224## Process Stack
225
226## Shell State
227
228## Completion
229
230### COMP_WORDS
231
232An array of words, split by : and = for compatibility with bash. New
233completion scripts should use COMP_ARGV instead.
234
235### COMP_CWORD
236
237Discouraged; for compatibility with bash.
238
239### COMP_LINE
240
241Discouraged; for compatibility with bash.
242
243### COMP_POINT
244
245Discouraged; for compatibility with bash.
246
247### COMP_WORDBREAKS
248
249Discouraged; for compatibility with bash.
250
251### COMPREPLY
252
253User-defined completion functions should Fill this array with candidates. It
254is cleared on every completion request.
255
256### COMP_ARGV
257
258An array of partial command arguments to complete. Preferred over COMP_WORDS.
259The compadjust builtin uses this variable.
260
261(An OSH extension to bash.)
262
263## History
264
265### HISTFILE
266
267Override the default OSH history location.
268
269### YSH_HISTFILE
270
271Override the default YSH history location.
272
273## cd
274
275### PWD
276
277### OLDPWD
278
279### CDPATH
280
281## getopts
282
283### OPTIND
284
285### OPTARG
286
287### OPTERR
288
289## read
290
291### REPLY
292
293OSH read sets this:
294
295 read < myfile
296
297## Functions
298
299### RANDOM
300
301bash compat
302
303### SECONDS
304
305bash compat
306