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