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