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

303 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
47An `Int` that's set by the `try` builtin.
48
49 try {
50 ls /bad # exits with status 2
51 }
52 if (_status !== 0) { # _status is 2
53 echo 'failed'
54 }
55
56### `_error`
57
58A `Dict` that's set by the `try` builtin when catching certain errors.
59
60Such errors include JSON/J8 encoding/decoding errors, and user errors from the
61`error` builtin.
62
63 try {
64 echo $[toJson( /d+/ )] # invalid Eggex type
65 }
66 echo "failed: $[_error.message]" # => failed: Can't serialize ...
67
68### `_pipeline_status`
69
70After a pipeline of processes is executed, this array contains the exit code of
71each process.
72
73Each exit code is an [Int](chap-type-method.html#Int). Compare with
74[`PIPESTATUS`](#PIPESTATUS).
75
76### `_process_sub_status`
77
78The exit status of all the process subs in the last command.
79
80## YSH Tracing
81
82### SHX_indent
83
84### SHX_punct
85
86### SHX_pid_str
87
88## YSH Read
89
90### _reply
91
92YSH `read` sets this variable:
93
94 read --all < myfile
95 echo $_reply
96
97## Oils VM
98
99### `OILS_VERSION`
100
101The version of Oils that's being run, e.g. `0.9.0`.
102
103<!-- TODO: specify comparison algorithm. -->
104
105### `OILS_GC_THRESHOLD`
106
107At a GC point, if there are more than this number of live objects, collect
108garbage.
109
110### `OILS_GC_ON_EXIT`
111
112Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
113exits. By default, we let the OS clean up.
114
115Useful for ASAN testing.
116
117### `OILS_GC_STATS`
118
119When the shell process exists, print GC stats to stderr.
120
121### `OILS_GC_STATS_FD`
122
123When the shell process exists, print GC stats to this file descriptor.
124
125## Shell Vars
126
127### IFS
128
129Used for word splitting. And the builtin `shSplit()` function.
130
131### LANG
132
133TODO: bash compat
134
135### GLOBIGNORE
136
137TODO: bash compat
138
139## Shell Options
140
141### SHELLOPTS
142
143bash compat: serialized options for the `set` builtin.
144
145### BASHOPTS
146
147bash compat: serialized options for the `shopt` builtin.
148
149## Other Env
150
151### HOME
152
153$HOME is used for:
154
1551. ~ expansion
1562. ~ abbreviation in the UI (the dirs builtin, \W in $PS1).
157
158Note: The shell doesn't set $HOME. According to POSIX, the program that
159invokes the login shell sets it based on /etc/passwd.
160
161### PATH
162
163A colon-separated string that's used to find executables to run.
164
165
166## POSIX Special
167
168## Other Special
169
170### BASH_REMATCH
171
172Result of regex evaluation `[[ $x =~ $pat ]]`.
173
174### PIPESTATUS
175
176After a pipeline of processes is executed, this array contains the exit code of
177each process.
178
179Each exit code is a [Str](chap-type-method.html#Str). Compare with
180[`_pipeline_status`](#_pipeline_status).
181
182## Platform
183
184### HOSTNAME
185
186The name of the "host" or machine that Oils is running on, determined by
187`gethostname()`.
188
189### OSTYPE
190
191The operating system that Oils is running on, determined by `uname()`.
192
193Examples: `linux darwin ...`
194
195## Call Stack
196
197### BASH_SOURCE
198
199### FUNCNAME
200
201### BASH_LINENO
202
203## Tracing
204
205### LINENO
206
207## Process State
208
209### BASHPID
210
211TODO
212
213### PPID
214
215TODO
216
217### UID
218
219### EUID
220
221## Process Stack
222
223## Shell State
224
225## Completion
226
227### COMP_WORDS
228
229An array of words, split by : and = for compatibility with bash. New
230completion scripts should use COMP_ARGV instead.
231
232### COMP_CWORD
233
234Discouraged; for compatibility with bash.
235
236### COMP_LINE
237
238Discouraged; for compatibility with bash.
239
240### COMP_POINT
241
242Discouraged; for compatibility with bash.
243
244### COMP_WORDBREAKS
245
246Discouraged; for compatibility with bash.
247
248### COMPREPLY
249
250User-defined completion functions should Fill this array with candidates. It
251is cleared on every completion request.
252
253### COMP_ARGV
254
255An array of partial command arguments to complete. Preferred over COMP_WORDS.
256The compadjust builtin uses this variable.
257
258(An OSH extension to bash.)
259
260## History
261
262### HISTFILE
263
264Override the default OSH history location.
265
266### YSH_HISTFILE
267
268Override the default YSH history location.
269
270## cd
271
272### PWD
273
274### OLDPWD
275
276### CDPATH
277
278## getopts
279
280### OPTIND
281
282### OPTARG
283
284### OPTERR
285
286## read
287
288### REPLY
289
290OSH read sets this:
291
292 read < myfile
293
294## Functions
295
296### RANDOM
297
298bash compat
299
300### SECONDS
301
302bash compat
303