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

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