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 | ARGV is List of strings contaning an argument array. This is the YSH
|
28 | replacement for shell's `"$@"`.
|
29 |
|
30 | - At the top level, it contains the arguments passed to the shell.
|
31 | - Inside an **open** [proc][], it contains the arguments passed to the proc.
|
32 |
|
33 | For example:
|
34 |
|
35 | proc p { # open proc, ARGV is swithout signature
|
36 | echo @ARGV
|
37 | }
|
38 | p a b c # => a b c
|
39 |
|
40 | In contrast, inside a closed proc, `ARGV` is empty.
|
41 |
|
42 | proc p () { # closed proc
|
43 | echo @ARGV
|
44 | }
|
45 |
|
46 | [proc]: chap-ysh-cmd.html#proc
|
47 |
|
48 | ### ENV
|
49 |
|
50 | An `Obj` that's populated with environment variables. Example usage:
|
51 |
|
52 | var x = ENV.PYTHONPATH
|
53 | echo $[ENV.SSH_AUTH_SOCK]
|
54 | setglobal ENV.PYTHONPATH = '.'
|
55 |
|
56 | It's initialized exactly **once** per process, in any of these situations:
|
57 |
|
58 | 1. At shell startup, if `shopt --set env_obj` is on. This is true when invoking
|
59 | `bin/ysh`.
|
60 | 2. When running `bin/osh -o ysh:upgrade` or `ysh:all`.
|
61 | 3. When running `shopt --set ysh:upgrade` or `ysh:all`.
|
62 |
|
63 | Related: [ysh-shopt][], [osh-usage][]
|
64 |
|
65 | [ysh-shopt]: chap-builtin-cmd.html#ysh-shopt
|
66 | [osh-usage]: chap-front-end.html#osh-usage
|
67 |
|
68 | ---
|
69 |
|
70 | When launching an external command, the shell creates a Unix `environ` from the
|
71 | `ENV` Obj. This means that mutating it affects all subsequent processes:
|
72 |
|
73 | setglobal ENV.PYTHONPATH = '.'
|
74 | ./foo.py
|
75 | ./bar.py
|
76 |
|
77 | You can also limit the change to a single process, without `ENV`:
|
78 |
|
79 | PYTHONPATH=. ./foo.py
|
80 | ./bar.py # unaffected
|
81 |
|
82 | ---
|
83 |
|
84 | YSH reads these ENV variables:
|
85 |
|
86 | - `PATH` - where to look for executables
|
87 | - `PS1` - how to print the prompt
|
88 | - TODO: `PS4` - how to show execution traces
|
89 | - `YSH_HISTFILE` (`HISTFILE` in OSH) - where to read/write command history
|
90 | - `HOME` - for tilde substitution ([tilde-sub])
|
91 |
|
92 | [tilde-sub]: chap-word-lang.html#tilde-sub
|
93 |
|
94 | ### `__defaults__`
|
95 |
|
96 | The shell puts some default settings in this `Dict`. In certain situations, it
|
97 | consults `__defaults__` after consulting `ENV`. For example:
|
98 |
|
99 | - if `ENV.PATH` is not set, consult `__defaults__.PATH`
|
100 | - if `ENV.PS1` is not set, consult `__defaults__.PS1`
|
101 |
|
102 | <!-- TODO: consider renaming to DEF.PS1 ? -->
|
103 |
|
104 | ### `__builtins__`
|
105 |
|
106 | An object that contains names visible in every module.
|
107 |
|
108 | If a name is not visible in the local scope, or module global scope, then it's
|
109 | looked up in `__builtins__`.
|
110 |
|
111 | ### `__sh_function__`
|
112 |
|
113 | A Dict that reflects the shell functions defined in the Oils interpreter.
|
114 |
|
115 | = keys(__sh_function__) # enumerate all shell functions
|
116 |
|
117 | = get(__sh_function__, 'f') # get a value of type Proc
|
118 |
|
119 | = __sh_function__ => get('f') # synonym for the above
|
120 |
|
121 | You can reflect on [doc comments][doc-comment] by calling the [docComment][]
|
122 | method on a value of type [Proc][].
|
123 |
|
124 | [doc-comment]: chap-front-end.html#doc-comment
|
125 | [docComment]: chap-type-method.html#docComment
|
126 | [Proc]: chap-type-method.html#Proc
|
127 |
|
128 | ### `_this_dir`
|
129 |
|
130 | The directory the current script resides in. This knows about 3 situations:
|
131 |
|
132 | - The location of `oshrc` in an interactive shell
|
133 | - The location of the main script, e.g. in `osh myscript.sh`
|
134 | - The location of script loaded with the `source` builtin
|
135 |
|
136 | It's useful for "relative imports".
|
137 |
|
138 | ## YSH Status
|
139 |
|
140 | ### `_status`
|
141 |
|
142 | DEPRECATED: Use `_error.code` instead.
|
143 |
|
144 | ### `_error`
|
145 |
|
146 | A `Dict` that's set by the `try` builtin.
|
147 |
|
148 | The integer `_error.code` is always present:
|
149 |
|
150 | try {
|
151 | ls /tmp
|
152 | }
|
153 | echo "status is $[_error.code]"
|
154 |
|
155 | Some errors also have a `message` field, like JSON/J8 encoding/decoding errors,
|
156 | and user errors from the [error][] builtin.
|
157 |
|
158 | try {
|
159 | echo $[toJson( /d+/ )] # invalid Eggex type
|
160 | }
|
161 | echo "failed: $[_error.message]" # => failed: Can't serialize ...
|
162 |
|
163 | [error]: chap-builtin-cmd.html#error
|
164 |
|
165 |
|
166 | ### `_pipeline_status`
|
167 |
|
168 | After a pipeline of processes is executed, this array contains the exit code of
|
169 | each process.
|
170 |
|
171 | Each exit code is an [Int](chap-type-method.html#Int). Compare with
|
172 | [`PIPESTATUS`](#PIPESTATUS).
|
173 |
|
174 | ### `_process_sub_status`
|
175 |
|
176 | The exit status of all the process subs in the last command.
|
177 |
|
178 | ## YSH Tracing
|
179 |
|
180 | ### SHX_indent
|
181 |
|
182 | ### SHX_punct
|
183 |
|
184 | ### SHX_pid_str
|
185 |
|
186 | ## YSH Read
|
187 |
|
188 | ### _reply
|
189 |
|
190 | Builtins that `read` set this variable:
|
191 |
|
192 | read --all < foo.txt
|
193 | = _reply # => 'contents of file'
|
194 |
|
195 | json read < foo.json
|
196 | = _reply # => (Dict) {}
|
197 |
|
198 | ## Oils VM
|
199 |
|
200 | ### `OILS_VERSION`
|
201 |
|
202 | The version of Oils that's being run, e.g. `0.23.0`.
|
203 |
|
204 | <!-- TODO: specify comparison algorithm. -->
|
205 |
|
206 | ### `LIB_OSH`
|
207 |
|
208 | The string `///osh`, which you can use with the [source][] builtin.
|
209 |
|
210 | source $LIB_OSH/two.sh
|
211 |
|
212 | [source]: chap-builtin-cmd.html#source
|
213 |
|
214 | ### `LIB_YSH`
|
215 |
|
216 | The string `///ysh`, which you can use with the [source][] builtin.
|
217 |
|
218 | source $LIB_YSH/yblocks.ysh
|
219 |
|
220 | [source]: chap-builtin-cmd.html#source
|
221 |
|
222 | ### `OILS_GC_THRESHOLD`
|
223 |
|
224 | At a GC point, if there are more than this number of live objects, collect
|
225 | garbage.
|
226 |
|
227 | ### `OILS_GC_ON_EXIT`
|
228 |
|
229 | Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
|
230 | exits. By default, we let the OS clean up.
|
231 |
|
232 | Useful for ASAN testing.
|
233 |
|
234 | ### `OILS_GC_STATS`
|
235 |
|
236 | When the shell process exists, print GC stats to stderr.
|
237 |
|
238 | ### `OILS_GC_STATS_FD`
|
239 |
|
240 | When the shell process exists, print GC stats to this file descriptor.
|
241 |
|
242 | ## Float
|
243 |
|
244 | ### NAN
|
245 |
|
246 | The float value for "not a number".
|
247 |
|
248 | (The name is consistent with the C language.)
|
249 |
|
250 | ### INFINITY
|
251 |
|
252 | The float value for "infinity". You can negate it to get "negative infinity".
|
253 |
|
254 | (The name is consistent with the C language.)
|
255 |
|
256 | ## Module
|
257 |
|
258 | ### `__provide__`
|
259 |
|
260 | A module is evaluated upon `use`. After evaluation, the names in the
|
261 | `__provide__` `List` are put in the resulting module `Obj` instance.
|
262 |
|
263 | <!--
|
264 | `__provide__` may also be a string, where 'p' stands for --procs, and 'f' stands for funcs.
|
265 |
|
266 | Or we could make it [1, 2] insetad
|
267 | -->
|
268 |
|
269 | ## POSIX Special
|
270 |
|
271 | $@ $* all arguments
|
272 | $0 ... $9 ${10} each argument
|
273 | $# the number of arguments
|
274 | $? last exit status
|
275 | $-
|
276 | $$ PID of shell (does not change when shell forks)
|
277 | $! ID of last job started, with & or bg
|
278 |
|
279 | Details on `$!`:
|
280 |
|
281 | - It's either
|
282 | - the PID of a process
|
283 | - the PID of the *last* part of the pipeline. (In contrast, the PGID of the
|
284 | pipeline is the PID of the first part.)
|
285 | - The [`wait`](chap-builtin-cmd.html#wait) builtin knows how to find a
|
286 | background pipeline job, given this ID.
|
287 | - It's set by:
|
288 | - [`ampersand &`](chap-cmd-lang.html#ampersand)
|
289 | - [`bg`](chap-builtin-cmd.html#bg)
|
290 | - In particular, it's not set after Ctrl-Z, only after `bg`.
|
291 |
|
292 | ## Shell Vars
|
293 |
|
294 | ### IFS
|
295 |
|
296 | Used for word splitting. And the builtin `shSplit()` function.
|
297 |
|
298 | ### LANG
|
299 |
|
300 | TODO: bash compat
|
301 |
|
302 | ### GLOBIGNORE
|
303 |
|
304 | TODO: bash compat
|
305 |
|
306 | ## Shell Options
|
307 |
|
308 | ### SHELLOPTS
|
309 |
|
310 | bash compat: serialized options for the `set` builtin.
|
311 |
|
312 | ### BASHOPTS
|
313 |
|
314 | bash compat: serialized options for the `shopt` builtin.
|
315 |
|
316 | (Not implemented.)
|
317 |
|
318 | ## Other Env
|
319 |
|
320 | ### HOME
|
321 |
|
322 | The `$HOME` env var is read by the shell, for:
|
323 |
|
324 | 1. `~` expansion
|
325 | 2. `~` abbreviation in the UI (the dirs builtin, `\W` in `$PS1`).
|
326 |
|
327 | The shell does not set $HOME. According to POSIX, the program that invokes the
|
328 | login shell should set it, based on `/etc/passwd`.
|
329 |
|
330 | ### PATH
|
331 |
|
332 | A colon-separated string that's used to find executables to run.
|
333 |
|
334 | In YSH, it's `ENV.PATH`.
|
335 |
|
336 | ## Other Special
|
337 |
|
338 | ### BASH_REMATCH
|
339 |
|
340 | Result of regex evaluation `[[ $x =~ $pat ]]`.
|
341 |
|
342 | ### PIPESTATUS
|
343 |
|
344 | After a pipeline of processes is executed, this array contains the exit code of
|
345 | each process.
|
346 |
|
347 | Each exit code is a [Str](chap-type-method.html#Str). Compare with
|
348 | [`_pipeline_status`](#_pipeline_status).
|
349 |
|
350 | ## Platform
|
351 |
|
352 | ### HOSTNAME
|
353 |
|
354 | The name of the "host" or machine that Oils is running on, determined by
|
355 | `gethostname()`.
|
356 |
|
357 | ### OSTYPE
|
358 |
|
359 | The operating system that Oils is running on, determined by `uname()`.
|
360 |
|
361 | Examples: `linux darwin ...`
|
362 |
|
363 | ## Call Stack
|
364 |
|
365 | ### BASH_SOURCE
|
366 |
|
367 | ### FUNCNAME
|
368 |
|
369 | ### BASH_LINENO
|
370 |
|
371 | ## Tracing
|
372 |
|
373 | ### LINENO
|
374 |
|
375 | ## Process State
|
376 |
|
377 | ### BASHPID
|
378 |
|
379 | TODO
|
380 |
|
381 | ### PPID
|
382 |
|
383 | TODO
|
384 |
|
385 | ### UID
|
386 |
|
387 | ### EUID
|
388 |
|
389 | ## Process Stack
|
390 |
|
391 | ## Shell State
|
392 |
|
393 | ## Completion
|
394 |
|
395 | ### COMP_WORDS
|
396 |
|
397 | An array of words, split by : and = for compatibility with bash. New
|
398 | completion scripts should use COMP_ARGV instead.
|
399 |
|
400 | ### COMP_CWORD
|
401 |
|
402 | Discouraged; for compatibility with bash.
|
403 |
|
404 | ### COMP_LINE
|
405 |
|
406 | Discouraged; for compatibility with bash.
|
407 |
|
408 | ### COMP_POINT
|
409 |
|
410 | Discouraged; for compatibility with bash.
|
411 |
|
412 | ### COMP_WORDBREAKS
|
413 |
|
414 | Discouraged; for compatibility with bash.
|
415 |
|
416 | ### COMPREPLY
|
417 |
|
418 | User-defined completion functions should Fill this array with candidates. It
|
419 | is cleared on every completion request.
|
420 |
|
421 | ### COMP_ARGV
|
422 |
|
423 | An array of partial command arguments to complete. Preferred over COMP_WORDS.
|
424 | The compadjust builtin uses this variable.
|
425 |
|
426 | (An OSH extension to bash.)
|
427 |
|
428 | ## History
|
429 |
|
430 | ### HISTFILE
|
431 |
|
432 | Override the default OSH history location.
|
433 |
|
434 | ### YSH_HISTFILE
|
435 |
|
436 | Override the default YSH history location.
|
437 |
|
438 | ## Interactive
|
439 |
|
440 | ### OILS_COMP_UI
|
441 |
|
442 | Set which completion UI to use. Defaults to `minimal`.
|
443 |
|
444 | - `minimal` - a UI that approximates the default behavior of GNU readline.
|
445 | - `nice` - a UI with a fancy pager and a prompt with horizontal scrolling instead of wrapping.
|
446 |
|
447 | This variable is currently only checked once during shell initialization.
|
448 |
|
449 | ## cd
|
450 |
|
451 | ### PWD
|
452 |
|
453 | ### OLDPWD
|
454 |
|
455 | ### CDPATH
|
456 |
|
457 | ## getopts
|
458 |
|
459 | ### OPTIND
|
460 |
|
461 | ### OPTARG
|
462 |
|
463 | ### OPTERR
|
464 |
|
465 | ## read
|
466 |
|
467 | ### REPLY
|
468 |
|
469 | OSH read sets this:
|
470 |
|
471 | read < myfile
|
472 |
|
473 | ## Functions
|
474 |
|
475 | ### RANDOM
|
476 |
|
477 | bash compat
|
478 |
|
479 | ### SECONDS
|
480 |
|
481 | bash compat
|
482 |
|