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