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