OILS / doc / getting-started.md View on Github | oils.pub

255 lines, 172 significant
1---
2default_highlighter: oils-sh
3---
4
5Getting Started
6===============
7
8There are many ways to use Oils!
9
10- You can use it *interactively*, or you can write "shell scripts" with it.
11 Shell is the best language for *ad hoc* automation.
12- You can use it in *compatible* mode with `bin/osh`, or in *legacy-free* mode
13 with `bin/ysh`.
14
15As of 2024, [OSH][] is mature, and [YSH][YSH] is under development. See [blog
16posts tagged #FAQ][blog-faqs] for more detail.
17
18[OSH]: https://oils.pub/cross-ref.html?tag=OSH#OSH
19[YSH]: https://oils.pub/cross-ref.html?tag=YSH#YSH
20
21This doc walks you through setting up Oils, explains some concepts, and links
22to more documentation.
23
24<div id="toc">
25</div>
26
27## Setup
28
29### Downloading Oils
30
31The [releases page](https://oils.pub/releases.html) links to source
32tarballs for every release. It also links to the documentation tree, which
33includes this page.
34
35### Your Configuration Dir
36
37After running the instructions in [INSTALL](INSTALL.html), run:
38
39 mkdir -p ~/.config/oils # for oshrc and yshrc
40 mkdir -p ~/.local/share/oils # for osh_history
41
42### Initialization with `rc` Files
43
44Note that
45
46- `bin/osh` runs `~/.config/oils/oshrc`
47- `bin/ysh` runs `~/.config/oils/yshrc`
48
49These are the **only** files that are "sourced". Other shells [have a
50confusing initialization sequence involving many files][mess] ([original][]).
51It's very hard to tell when and if `/etc/profile`, `~/.bashrc`,
52`~/.bash_profile`, etc. are executed.
53
54OSH and YSH intentionally avoid this. If you want those files, simply `source`
55them in your `oshrc`.
56
57- Related: the [config][] reference topic.
58
59[config]: ref/chap-front-end.html#config
60
61[mess]: https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/
62
63[original]: http://www.solipsys.co.uk/new/BashInitialisationFiles.html
64
65I describe my own `oshrc` file on the Wiki: [How To Test
66OSH](https://github.com/ols-for-unix/oils/wiki/How-To-Test-OSH).
67
68### Setting the Prompt
69
70OSH supports the `$PS1` variable, with bash-style escape codes. See [PS1][] for details.
71
72Example:
73
74 # oshrc
75 PS1='\s '
76
77YSH has the [renderPrompt()][renderPrompt] hook, which is a YSH function. It
78often uses [io.promptVal()][promptVal].
79
80Example:
81
82 # yshrc
83 func renderPrompt(io) {
84 return (io.promptVal('s') ++ ' ')
85 }
86
87[PS1]: ref/chap-plugin.html#PS1
88[renderPrompt]: ref/chap-plugin.html#renderPrompt
89[promptVal]: ref/chap-type-method.html#promptVal
90
91### Editor Support
92
93See [YSH Editor Support]($wiki) on the wiki.
94
95## Getting Help
96
97Type `help` in `osh` or `ysh`, which links to URLs in the [Oils
98Reference](ref/index.html).
99
100## Tips
101
102- If you get tired of typing `~/.config/oils/oshrc`, symlink it to `~/.oshrc`.
103
104### Troubleshooting
105
106- If you're running OSH from `bash` or `zsh`, then the prompt string `$PS1` may
107 be unintentionally inherited. Running `PS1=''` before `bin/osh` avoids this.
108 This is also true for `$PS2`, `$PS4`, etc.
109- On Arch Linux and other distros,`$LANG` may not get set without
110 `/etc/profile`. Adding `source /etc/profile` to your `oshrc` may solve this
111 problem.
112- See [OSH Compatibility Tips](https://github.com/oils-for-unix/oils/wiki/OSH-Compatibility-Tips) to configure programs that rely on `eval` to initialize (e.g. starship, zoxide).
113
114### `sh` and Bash Docs Are Useful for OSH
115
116Existing educational materials for the Unix shell apply to OSH, because they
117generally don't teach the quirks that OSH disallows. For example, much of the
118information and advice in [BashGuide][] can be used without worrying about
119which shell you're using. See the end of this manual for more resources.
120
121For this reason, we're focusing efforts on documenting [YSH][].
122
123## What Is Expected to Run Under OSH
124
125"Batch" programs are most likely to run unmodified under OSH. On the other
126hand, Interactive programs like `.bashrc` and bash completion scripts may
127require small changes.
128
129- Wiki: [What Is Expected to Run Under OSH]($wiki)
130
131## Features Unique to OSH
132
133### Dumping the AST
134
135The `-n` flag tells OSH to parse the program rather than executing it. By
136default, it prints an abbreviated abstract syntax tree:
137
138 $ bin/osh -n -c 'ls | wc -l'
139 (command.Pipeline children:[(C {(ls)}) (C {(wc)} {(-l)})] negated:F)
140
141You can also ask for the full `text` format:
142
143 $ bin/osh -n --ast-format text -c 'ls | wc -l'
144 (command.Pipeline
145 children: [
146 (command.Simple
147 words: [
148 (word.Compound
149 parts: [(word_part.Literal
150 token:(token id:Lit_Chars val:ls span_id:0))]
151 )
152 ]
153 )
154 (command.Simple
155 words: [
156 (word.Compound
157 parts: [(word_part.Literal
158 token:(token id:Lit_Chars val:wc span_id:4))]
159 )
160 (word.Compound
161 parts: [(word_part.Literal
162 token:(token id:Lit_Chars val:-l span_id:6))]
163 )
164 ]
165 )
166 ]
167 negated: F
168 spids: [2]
169 )
170
171This format is **subject to change**. It's there for debugging the parser, but
172sophisticated users may use it to interpret tricky shell programs without
173running them.
174
175
176### `OILS_HIJACK_SHEBANG`
177
178This environment variable can be set to the path of a **shell**. Before OSH
179executes a program, it will inspect the shebang line to see if it looks like a
180shell script. If it does, it will use this shell instead of the one specified
181in the shebang line.
182
183For example, suppose you have `myscript.sh`:
184
185 #!/bin/sh
186 # myscript.sh
187
188 ./otherscript.sh --flag ...
189
190and `otherscript.sh`:
191
192 #!/bin/sh
193 # otherscript.sh
194
195 echo 'hello world'
196
197Then you can run `myscript.sh` like this:
198
199 OILS_HIJACK_SHEBANG=osh osh myscript.sh
200
201and `otherscript.sh` will be executed with OSH rather than the `/bin/sh`.
202
203Note that `osh` appears **twice** in that command line: once for the initial
204run, and once for all recursive runs.
205
206(This is an environment variable rather than a flag because it needs to be
207**inherited**.)
208
209### `--debug-file`
210
211Print internal debug logs to this file. It's useful to make it a FIFO:
212
213 mkfifo _tmp/debug
214 osh --debug-file _tmp/debug
215
216Then run this in another window to see logs as you type:
217
218 cat _tmp/debug
219
220Related:
221
222- The `OSH_DEBUG_DIR` environment variable is the inherited version of
223 `--debug-file`. A file named `$PID-osh.log` will be written in that
224 directory for every shell process.
225- The `--xtrace-to-debug-file` flag sends `set -o xtrace` output to that file
226 instead of to `stderr`.
227
228<!--
229### Crash Dumps
230
231- TODO: `OILS_CRASH_DUMP_DIR`
232-->
233
234## Appendix
235
236### Links
237
238- [Blog Posts Tagged #FAQ][blog-faqs]
239 tell you why OSH exists and how it's designed.
240- [Known Differences](known-differences.html) lists incompatibilities between
241 OSH and other shells. They are unlikely to appear in real programs, or
242 there is a trivial workaround.
243
244[blog-faqs]: https://www.oilshell.org/blog/tags.html?tag=FAQ#FAQ
245
246External:
247
248- [GNU Bash Manual](https://www.gnu.org/software/bash/manual/). I frequently
249 referred to this document when implementing OSH.
250- [BashGuide][]. A wiki with detailed information about bash.
251 - [BashPitfalls](https://mywiki.wooledge.org/BashPitfalls).
252- [Bash Cheat Sheet](https://devhints.io/bash). A short overview.
253
254[BashGuide]: https://mywiki.wooledge.org/BashGuide
255