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 | $HOME is used for:
|
215 |
|
216 | 1. ~ expansion
|
217 | 2. ~ abbreviation in the UI (the dirs builtin, \W in $PS1).
|
218 |
|
219 | Note: The shell doesn't set $HOME. According to POSIX, the program that
|
220 | invokes the login shell sets it based on /etc/passwd.
|
221 |
|
222 | ### PATH
|
223 |
|
224 | A colon-separated string that's used to find executables to run.
|
225 |
|
226 |
|
227 | ## POSIX Special
|
228 |
|
229 | ## Other Special
|
230 |
|
231 | ### BASH_REMATCH
|
232 |
|
233 | Result of regex evaluation `[[ $x =~ $pat ]]`.
|
234 |
|
235 | ### PIPESTATUS
|
236 |
|
237 | After a pipeline of processes is executed, this array contains the exit code of
|
238 | each process.
|
239 |
|
240 | Each exit code is a [Str](chap-type-method.html#Str). Compare with
|
241 | [`_pipeline_status`](#_pipeline_status).
|
242 |
|
243 | ## Platform
|
244 |
|
245 | ### HOSTNAME
|
246 |
|
247 | The name of the "host" or machine that Oils is running on, determined by
|
248 | `gethostname()`.
|
249 |
|
250 | ### OSTYPE
|
251 |
|
252 | The operating system that Oils is running on, determined by `uname()`.
|
253 |
|
254 | Examples: `linux darwin ...`
|
255 |
|
256 | ## Call Stack
|
257 |
|
258 | ### BASH_SOURCE
|
259 |
|
260 | ### FUNCNAME
|
261 |
|
262 | ### BASH_LINENO
|
263 |
|
264 | ## Tracing
|
265 |
|
266 | ### LINENO
|
267 |
|
268 | ## Process State
|
269 |
|
270 | ### BASHPID
|
271 |
|
272 | TODO
|
273 |
|
274 | ### PPID
|
275 |
|
276 | TODO
|
277 |
|
278 | ### UID
|
279 |
|
280 | ### EUID
|
281 |
|
282 | ## Process Stack
|
283 |
|
284 | ## Shell State
|
285 |
|
286 | ## Completion
|
287 |
|
288 | ### COMP_WORDS
|
289 |
|
290 | An array of words, split by : and = for compatibility with bash. New
|
291 | completion scripts should use COMP_ARGV instead.
|
292 |
|
293 | ### COMP_CWORD
|
294 |
|
295 | Discouraged; for compatibility with bash.
|
296 |
|
297 | ### COMP_LINE
|
298 |
|
299 | Discouraged; for compatibility with bash.
|
300 |
|
301 | ### COMP_POINT
|
302 |
|
303 | Discouraged; for compatibility with bash.
|
304 |
|
305 | ### COMP_WORDBREAKS
|
306 |
|
307 | Discouraged; for compatibility with bash.
|
308 |
|
309 | ### COMPREPLY
|
310 |
|
311 | User-defined completion functions should Fill this array with candidates. It
|
312 | is cleared on every completion request.
|
313 |
|
314 | ### COMP_ARGV
|
315 |
|
316 | An array of partial command arguments to complete. Preferred over COMP_WORDS.
|
317 | The compadjust builtin uses this variable.
|
318 |
|
319 | (An OSH extension to bash.)
|
320 |
|
321 | ## History
|
322 |
|
323 | ### HISTFILE
|
324 |
|
325 | Override the default OSH history location.
|
326 |
|
327 | ### YSH_HISTFILE
|
328 |
|
329 | Override the default YSH history location.
|
330 |
|
331 | ## cd
|
332 |
|
333 | ### PWD
|
334 |
|
335 | ### OLDPWD
|
336 |
|
337 | ### CDPATH
|
338 |
|
339 | ## getopts
|
340 |
|
341 | ### OPTIND
|
342 |
|
343 | ### OPTARG
|
344 |
|
345 | ### OPTERR
|
346 |
|
347 | ## read
|
348 |
|
349 | ### REPLY
|
350 |
|
351 | OSH read sets this:
|
352 |
|
353 | read < myfile
|
354 |
|
355 | ## Functions
|
356 |
|
357 | ### RANDOM
|
358 |
|
359 | bash compat
|
360 |
|
361 | ### SECONDS
|
362 |
|
363 | bash compat
|
364 |
|