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 | TODO
|
32 |
|
33 | ### _this_dir
|
34 |
|
35 | The directory the current script resides in. This knows about 3 situations:
|
36 |
|
37 | - The location of `oshrc` in an interactive shell
|
38 | - The location of the main script, e.g. in `osh myscript.sh`
|
39 | - The location of script loaded with the `source` builtin
|
40 |
|
41 | It's useful for "relative imports".
|
42 |
|
43 | ## YSH Status
|
44 |
|
45 | ### `_status`
|
46 |
|
47 | An `Int` that's set by the `try` builtin.
|
48 |
|
49 | try {
|
50 | ls /bad # exits with status 2
|
51 | }
|
52 | if (_status !== 0) { # _status is 2
|
53 | echo 'failed'
|
54 | }
|
55 |
|
56 | ### `_error`
|
57 |
|
58 | A `Dict` that's set by the `try` builtin when catching certain errors.
|
59 |
|
60 | Such errors include JSON/J8 encoding/decoding errors, and user errors from the
|
61 | `error` builtin.
|
62 |
|
63 | try {
|
64 | echo $[toJson( /d+/ )] # invalid Eggex type
|
65 | }
|
66 | echo "failed: $[_error.message]" # => failed: Can't serialize ...
|
67 |
|
68 | ### `_pipeline_status`
|
69 |
|
70 | After a pipeline of processes is executed, this array contains the exit code of
|
71 | each process.
|
72 |
|
73 | Each exit code is an [Int](chap-type-method.html#Int). Compare with
|
74 | [`PIPESTATUS`](#PIPESTATUS).
|
75 |
|
76 | ### `_process_sub_status`
|
77 |
|
78 | The exit status of all the process subs in the last command.
|
79 |
|
80 | ## YSH Tracing
|
81 |
|
82 | ### SHX_indent
|
83 |
|
84 | ### SHX_punct
|
85 |
|
86 | ### SHX_pid_str
|
87 |
|
88 | ## YSH Read
|
89 |
|
90 | ### _reply
|
91 |
|
92 | YSH `read` sets this variable:
|
93 |
|
94 | read --all < myfile
|
95 | echo $_reply
|
96 |
|
97 | ## Oils VM
|
98 |
|
99 | ### `OILS_VERSION`
|
100 |
|
101 | The version of Oils that's being run, e.g. `0.9.0`.
|
102 |
|
103 | <!-- TODO: specify comparison algorithm. -->
|
104 |
|
105 | ### `OILS_GC_THRESHOLD`
|
106 |
|
107 | At a GC point, if there are more than this number of live objects, collect
|
108 | garbage.
|
109 |
|
110 | ### `OILS_GC_ON_EXIT`
|
111 |
|
112 | Set `OILS_GC_ON_EXIT=1` to explicitly collect and `free()` before the process
|
113 | exits. By default, we let the OS clean up.
|
114 |
|
115 | Useful for ASAN testing.
|
116 |
|
117 | ### `OILS_GC_STATS`
|
118 |
|
119 | When the shell process exists, print GC stats to stderr.
|
120 |
|
121 | ### `OILS_GC_STATS_FD`
|
122 |
|
123 | When the shell process exists, print GC stats to this file descriptor.
|
124 |
|
125 | ## Shell Vars
|
126 |
|
127 | ### IFS
|
128 |
|
129 | Used for word splitting. And the builtin `shSplit()` function.
|
130 |
|
131 | ### LANG
|
132 |
|
133 | TODO: bash compat
|
134 |
|
135 | ### GLOBIGNORE
|
136 |
|
137 | TODO: bash compat
|
138 |
|
139 | ## Shell Options
|
140 |
|
141 | ### SHELLOPTS
|
142 |
|
143 | bash compat: serialized options for the `set` builtin.
|
144 |
|
145 | ### BASHOPTS
|
146 |
|
147 | bash compat: serialized options for the `shopt` builtin.
|
148 |
|
149 | ## Other Env
|
150 |
|
151 | ### HOME
|
152 |
|
153 | $HOME is used for:
|
154 |
|
155 | 1. ~ expansion
|
156 | 2. ~ abbreviation in the UI (the dirs builtin, \W in $PS1).
|
157 |
|
158 | Note: The shell doesn't set $HOME. According to POSIX, the program that
|
159 | invokes the login shell sets it based on /etc/passwd.
|
160 |
|
161 | ### PATH
|
162 |
|
163 | A colon-separated string that's used to find executables to run.
|
164 |
|
165 |
|
166 | ## POSIX Special
|
167 |
|
168 | ## Other Special
|
169 |
|
170 | ### BASH_REMATCH
|
171 |
|
172 | Result of regex evaluation `[[ $x =~ $pat ]]`.
|
173 |
|
174 | ### PIPESTATUS
|
175 |
|
176 | After a pipeline of processes is executed, this array contains the exit code of
|
177 | each process.
|
178 |
|
179 | Each exit code is a [Str](chap-type-method.html#Str). Compare with
|
180 | [`_pipeline_status`](#_pipeline_status).
|
181 |
|
182 | ## Platform
|
183 |
|
184 | ### HOSTNAME
|
185 |
|
186 | The name of the "host" or machine that Oils is running on, determined by
|
187 | `gethostname()`.
|
188 |
|
189 | ### OSTYPE
|
190 |
|
191 | The operating system that Oils is running on, determined by `uname()`.
|
192 |
|
193 | Examples: `linux darwin ...`
|
194 |
|
195 | ## Call Stack
|
196 |
|
197 | ### BASH_SOURCE
|
198 |
|
199 | ### FUNCNAME
|
200 |
|
201 | ### BASH_LINENO
|
202 |
|
203 | ## Tracing
|
204 |
|
205 | ### LINENO
|
206 |
|
207 | ## Process State
|
208 |
|
209 | ### BASHPID
|
210 |
|
211 | TODO
|
212 |
|
213 | ### PPID
|
214 |
|
215 | TODO
|
216 |
|
217 | ### UID
|
218 |
|
219 | ### EUID
|
220 |
|
221 | ## Process Stack
|
222 |
|
223 | ## Shell State
|
224 |
|
225 | ## Completion
|
226 |
|
227 | ### COMP_WORDS
|
228 |
|
229 | An array of words, split by : and = for compatibility with bash. New
|
230 | completion scripts should use COMP_ARGV instead.
|
231 |
|
232 | ### COMP_CWORD
|
233 |
|
234 | Discouraged; for compatibility with bash.
|
235 |
|
236 | ### COMP_LINE
|
237 |
|
238 | Discouraged; for compatibility with bash.
|
239 |
|
240 | ### COMP_POINT
|
241 |
|
242 | Discouraged; for compatibility with bash.
|
243 |
|
244 | ### COMP_WORDBREAKS
|
245 |
|
246 | Discouraged; for compatibility with bash.
|
247 |
|
248 | ### COMPREPLY
|
249 |
|
250 | User-defined completion functions should Fill this array with candidates. It
|
251 | is cleared on every completion request.
|
252 |
|
253 | ### COMP_ARGV
|
254 |
|
255 | An array of partial command arguments to complete. Preferred over COMP_WORDS.
|
256 | The compadjust builtin uses this variable.
|
257 |
|
258 | (An OSH extension to bash.)
|
259 |
|
260 | ## History
|
261 |
|
262 | ### HISTFILE
|
263 |
|
264 | Override the default OSH history location.
|
265 |
|
266 | ### YSH_HISTFILE
|
267 |
|
268 | Override the default YSH history location.
|
269 |
|
270 | ## cd
|
271 |
|
272 | ### PWD
|
273 |
|
274 | ### OLDPWD
|
275 |
|
276 | ### CDPATH
|
277 |
|
278 | ## getopts
|
279 |
|
280 | ### OPTIND
|
281 |
|
282 | ### OPTARG
|
283 |
|
284 | ### OPTERR
|
285 |
|
286 | ## read
|
287 |
|
288 | ### REPLY
|
289 |
|
290 | OSH read sets this:
|
291 |
|
292 | read < myfile
|
293 |
|
294 | ## Functions
|
295 |
|
296 | ### RANDOM
|
297 |
|
298 | bash compat
|
299 |
|
300 | ### SECONDS
|
301 |
|
302 | bash compat
|
303 |
|