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