1 | ---
|
2 | title: Special Variables (Oils Reference)
|
3 | all_docs_url: ..
|
4 | body_css_class: width40
|
5 | default_highlighter: oils-sh
|
6 | preserve_anchor_case: yes
|
7 | ---
|
8 |
|
9 | <div class="doc-ref-header">
|
10 |
|
11 | [Oils Reference](index.html) —
|
12 | Chapter **Special Variables**
|
13 |
|
14 | </div>
|
15 |
|
16 | This 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 |
|
27 | Replacement for `"$@"`
|
28 |
|
29 | ### ENV
|
30 |
|
31 | A Dict that's populated with environment variables. Example usage:
|
32 |
|
33 | var x = ENV.PYTHONPATH
|
34 | echo $[ENV.SSH_AUTH_SOCK]
|
35 |
|
36 | It's initialized exactly **once** per process, in any of these situations:
|
37 |
|
38 | 1. At shell startup, if `shopt --set env_obj` is on. This is true when invoking
|
39 | `bin/ysh`.
|
40 | 2. When running `bin/osh -o ysh:upgrade` or `ysh:all`.
|
41 | 3. When running `shopt --set ysh:upgrade` or `ysh:all`.
|
42 |
|
43 | Related: [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 |
|
50 | The 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 |
|
56 | It's useful for "relative imports".
|
57 |
|
58 | ## YSH Status
|
59 |
|
60 | ### `_status`
|
61 |
|
62 | DEPRECATED: Use `_error.code` instead.
|
63 |
|
64 | ### `_error`
|
65 |
|
66 | A `Dict` that's set by the `try` builtin.
|
67 |
|
68 | The integer `_error.code` is always present:
|
69 |
|
70 | try {
|
71 | ls /tmp
|
72 | }
|
73 | echo "status is $[_error.code]"
|
74 |
|
75 | Some errors also have a `message` field, like JSON/J8 encoding/decoding errors,
|
76 | and 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 |
|
88 | After a pipeline of processes is executed, this array contains the exit code of
|
89 | each process.
|
90 |
|
91 | Each exit code is an [Int](chap-type-method.html#Int). Compare with
|
92 | [`PIPESTATUS`](#PIPESTATUS).
|
93 |
|
94 | ### `_process_sub_status`
|
95 |
|
96 | The 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 |
|
110 | YSH `read` sets this variable:
|
111 |
|
112 | read --all < myfile
|
113 | echo $_reply
|
114 |
|
115 | ## Oils VM
|
116 |
|
117 | ### `OILS_VERSION`
|
118 |
|
119 | The version of Oils that's being run, e.g. `0.23.0`.
|
120 |
|
121 | <!-- TODO: specify comparison algorithm. -->
|
122 |
|
123 | ### `LIB_OSH`
|
124 |
|
125 | The 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 |
|
133 | The 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 |
|
141 | At a GC point, if there are more than this number of live objects, collect
|
142 | garbage.
|
143 |
|
144 | ### `OILS_GC_ON_EXIT`
|
145 |
|
146 | Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
|
147 | exits. By default, we let the OS clean up.
|
148 |
|
149 | Useful for ASAN testing.
|
150 |
|
151 | ### `OILS_GC_STATS`
|
152 |
|
153 | When the shell process exists, print GC stats to stderr.
|
154 |
|
155 | ### `OILS_GC_STATS_FD`
|
156 |
|
157 | When the shell process exists, print GC stats to this file descriptor.
|
158 |
|
159 | ## Float
|
160 |
|
161 | ### NAN
|
162 |
|
163 | The float value for "not a number".
|
164 |
|
165 | (The name is consistent with the C language.)
|
166 |
|
167 | ### INFINITY
|
168 |
|
169 | The 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 |
|
177 | A 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 |
|
183 | Or we could make it [1, 2] insetad
|
184 | -->
|
185 |
|
186 | ## Shell Vars
|
187 |
|
188 | ### IFS
|
189 |
|
190 | Used for word splitting. And the builtin `shSplit()` function.
|
191 |
|
192 | ### LANG
|
193 |
|
194 | TODO: bash compat
|
195 |
|
196 | ### GLOBIGNORE
|
197 |
|
198 | TODO: bash compat
|
199 |
|
200 | ## Shell Options
|
201 |
|
202 | ### SHELLOPTS
|
203 |
|
204 | bash compat: serialized options for the `set` builtin.
|
205 |
|
206 | ### BASHOPTS
|
207 |
|
208 | bash compat: serialized options for the `shopt` builtin.
|
209 |
|
210 | ## Other Env
|
211 |
|
212 | ### HOME
|
213 |
|
214 | The `$HOME` env var is read by the shell, for:
|
215 |
|
216 | 1. `~` expansion
|
217 | 2. `~` abbreviation in the UI (the dirs builtin, `\W` in `$PS1`).
|
218 |
|
219 | The shell does not set $HOME. According to POSIX, the program that invokes the
|
220 | login shell should set it, based on `/etc/passwd`.
|
221 |
|
222 | ### PATH
|
223 |
|
224 | A colon-separated string that's used to find executables to run.
|
225 |
|
226 | In YSH, it's `ENV.PATH`.
|
227 |
|
228 | ## POSIX Special
|
229 |
|
230 | ## Other Special
|
231 |
|
232 | ### BASH_REMATCH
|
233 |
|
234 | Result of regex evaluation `[[ $x =~ $pat ]]`.
|
235 |
|
236 | ### PIPESTATUS
|
237 |
|
238 | After a pipeline of processes is executed, this array contains the exit code of
|
239 | each process.
|
240 |
|
241 | Each exit code is a [Str](chap-type-method.html#Str). Compare with
|
242 | [`_pipeline_status`](#_pipeline_status).
|
243 |
|
244 | ## Platform
|
245 |
|
246 | ### HOSTNAME
|
247 |
|
248 | The name of the "host" or machine that Oils is running on, determined by
|
249 | `gethostname()`.
|
250 |
|
251 | ### OSTYPE
|
252 |
|
253 | The operating system that Oils is running on, determined by `uname()`.
|
254 |
|
255 | Examples: `linux darwin ...`
|
256 |
|
257 | ## Call Stack
|
258 |
|
259 | ### BASH_SOURCE
|
260 |
|
261 | ### FUNCNAME
|
262 |
|
263 | ### BASH_LINENO
|
264 |
|
265 | ## Tracing
|
266 |
|
267 | ### LINENO
|
268 |
|
269 | ## Process State
|
270 |
|
271 | ### BASHPID
|
272 |
|
273 | TODO
|
274 |
|
275 | ### PPID
|
276 |
|
277 | TODO
|
278 |
|
279 | ### UID
|
280 |
|
281 | ### EUID
|
282 |
|
283 | ## Process Stack
|
284 |
|
285 | ## Shell State
|
286 |
|
287 | ## Completion
|
288 |
|
289 | ### COMP_WORDS
|
290 |
|
291 | An array of words, split by : and = for compatibility with bash. New
|
292 | completion scripts should use COMP_ARGV instead.
|
293 |
|
294 | ### COMP_CWORD
|
295 |
|
296 | Discouraged; for compatibility with bash.
|
297 |
|
298 | ### COMP_LINE
|
299 |
|
300 | Discouraged; for compatibility with bash.
|
301 |
|
302 | ### COMP_POINT
|
303 |
|
304 | Discouraged; for compatibility with bash.
|
305 |
|
306 | ### COMP_WORDBREAKS
|
307 |
|
308 | Discouraged; for compatibility with bash.
|
309 |
|
310 | ### COMPREPLY
|
311 |
|
312 | User-defined completion functions should Fill this array with candidates. It
|
313 | is cleared on every completion request.
|
314 |
|
315 | ### COMP_ARGV
|
316 |
|
317 | An array of partial command arguments to complete. Preferred over COMP_WORDS.
|
318 | The compadjust builtin uses this variable.
|
319 |
|
320 | (An OSH extension to bash.)
|
321 |
|
322 | ## History
|
323 |
|
324 | ### HISTFILE
|
325 |
|
326 | Override the default OSH history location.
|
327 |
|
328 | ### YSH_HISTFILE
|
329 |
|
330 | Override the default YSH history location.
|
331 |
|
332 | ## cd
|
333 |
|
334 | ### PWD
|
335 |
|
336 | ### OLDPWD
|
337 |
|
338 | ### CDPATH
|
339 |
|
340 | ## getopts
|
341 |
|
342 | ### OPTIND
|
343 |
|
344 | ### OPTARG
|
345 |
|
346 | ### OPTERR
|
347 |
|
348 | ## read
|
349 |
|
350 | ### REPLY
|
351 |
|
352 | OSH read sets this:
|
353 |
|
354 | read < myfile
|
355 |
|
356 | ## Functions
|
357 |
|
358 | ### RANDOM
|
359 |
|
360 | bash compat
|
361 |
|
362 | ### SECONDS
|
363 |
|
364 | bash compat
|
365 |
|