Skip to content
xoPublic

About

A Unix shell in pure Go that runs bash scripts and replaces bash at the terminal, with TinyGo and WebAssembly builds

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

74 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hail

hail is a bash shell written in pure Go. It runs your bash scripts, and you can use it as your shell at the terminal. It is one static binary with no C library, for Linux, macOS, Windows, FreeBSD and NetBSD, and it also runs as WebAssembly.

hail colors a for loop and a pipeline in the style onedark as they are typed, runs them, and completes a command, a variable and a file name with Tab


Unit Tests Go Reference Releases Discord Discussion

What hail gives you

  • hail gives the output, the errors and the exit status of GNU bash 5.3. On the tests of GNU bash 5.3, hail gives the answers of bash on 84 of 86 test files, and brush 0.5.0 on 12. On the 3,740 cases of brush that test what bash does, checked against the answers of bash, hail passes 3,730 and brush 2,862.
  • hail uses no cgo and links no C library. It runs natively on Windows, without MSYS or Cygwin, and it builds with TinyGo and for WebAssembly.
  • The interactive shell has line editing in emacs or vi mode, your inputrc, history, Tab completion that works with bash-completion, colors as you type in any of 168 styles, and job control.
  • A script behaves the same on every machine, because hail reads no bashrc. Your settings go in config.yaml, which only the interactive shell reads.

hail started as a port of brush, a bash written in Rust, and follows bash where the two differ. It is also the full example of three xo packages: ox for the command line, rline for line editing, and transit for syntax highlighting.

Installing

Build hail from source with Go, with cgo turned off, so that the binary is static:

git clone https://github.com/xo/hail.git
cd hail
CGO_ENABLED=0 go build .

On Linux, you can also build hail with TinyGo. Give TinyGo the flag -stack-size=8MB, which hail needs for a deep recursion (D28):

tinygo build -stack-size=8MB .

Each release on the Releases page will hold builds for Linux, macOS and Windows on amd64 and arm64, for FreeBSD and NetBSD on amd64, and the builds of TinyGo for WASI and for the browser. There is no release yet.

The texts of help in GNU bash are under the GPL, so hail gives a short text of its own for each builtin. CONTRIBUTING.md says how to make a build with the texts of bash (D38).

Using hail

hail takes the command line of bash 5.3:

hail                         # an interactive shell
hail script.sh arg1 arg2     # run a script
hail -c 'echo "$1"' hail hi  # run a command
hail -n script.sh            # check a script without running it
hail --pretty-print x.sh     # print a script as bash prints it

hail --help lists the options. hail does not support -D and --dump-po-strings yet, and ends with status 2 when it gets one of them.

To run a script with hail, put hail in its first line, or run it with hail script.sh:

#!/usr/bin/env hail

$BASH_VERSION reports the release of bash that hail follows. A script that must know that hail runs it tests $HAIL_VERSION.

Using hail as bash

Started under the name bash, hail acts as bash. It reads the startup files of bash in place of config.yaml: /etc/profile and the first of ~/.bash_profile, ~/.bash_login and ~/.profile for a login shell, and ~/.bashrc for an interactive shell that is not a login shell. A login shell runs ~/.bash_logout when it ends. hail does not read /etc/bash.bashrc, which some systems build into their bash. To try it from bash:

exec -a bash hail

A link named bash to hail does the same. Do not put such a link before the real bash in PATH, because every script that runs bash then runs hail.

Under the name sh, hail turns on POSIX mode, reads /etc/profile and ~/.profile for a login shell, and reads the file that $ENV names for an interactive shell. Under the name rbash, or with -r, hail is a restricted shell, as bash is. A restricted shell is not a sandbox (D39).

Under its own name, hail reads no bash startup file. To try your bash setup in hail, source it at the prompt:

source ~/.bashrc

Configuration

An interactive hail reads config.yaml from its configuration directory. A script that hail runs does not read it:

Platform Directory
Linux and other Unix systems $XDG_CONFIG_HOME/hail, which is ~/.config/hail by default
macOS ~/Library/Application Support/hail
Windows %AppData%\hail

The file holds settings, and not code. Every key is optional:

prompt: '\u@\h:\w\$ '    # PS1
prompt2: '> '             # PS2
env:                      # variables to set and export
  EDITOR: vim
set: [noclobber]          # options of set -o
shopt: [globstar]         # options of shopt
aliases:
  ll: ls -l
history:
  file: ~/.local/state/hail/history   # HISTFILE
  size: 10000                         # HISTSIZE
  file_size: 20000                    # HISTFILESIZE
  control: [ignoreboth]               # HISTCONTROL
  ignore: [ls, bg]                    # HISTIGNORE
editor:
  vi: false               # edit in vi mode, as set -o vi does
  color: true             # color the line as you type it
  style: onedark          # the style of the colors, by name or path
  styles:                 # the style of a kind of word, over the style
    keyword: 'bold #c678dd'
  bindings:               # key bindings, as lines of an inputrc file
    - '"\C-o": "> output"'
    - 'set completion-ignore-case on'
source:                   # scripts to run, relative to the directory of the file
  - functions.sh

Put functions in a bash script, and name the script under source. --norc turns the file off.

hail applies the settings in this order: env, the prompts and the history, set, shopt, aliases, editor.vi, editor.style, editor.bindings, and then the scripts of source. A script can change what the keys set.

hail reads your inputrc file first, and then the lines of editor.bindings, as one more inputrc file. So a binding in config.yaml replaces a binding of the same keys in your inputrc. Each line is a line that you can give to bind. Write each line in single quotes, so that YAML keeps each backslash.

Styles

hail colors the line as you type it, with a style of transit. It bundles the 168 styles of transit, and the default style is onedark.

A for loop on the line of hail, in the colors of onedark: the keywords in purple, the commands in bold blue, the strings in green and the variable in red

To list the styles, and to see one before you choose it, run:

hail --list-styles
hail --preview-style dracula

To choose a style, name it under editor.style. editor.style also takes the path of a JSON style file in the form of transit, relative to the directory of config.yaml. editor.styles changes the style of one kind of word: comment, constant, embedded, function, keyword, number, operator, property or string. An empty value takes the color off. Put a value that holds # in quotes, because YAML starts a comment at #:

editor:
  style: dracula
  styles:
    comment: 'italic #6272a4'
    keyword: bold ansi-fuchsia
    string: ''

hail draws each color as the style gives it when COLORTERM is truecolor or 24bit, and the nearest color on a terminal of 256 or 16 colors. It keeps the background of the terminal, so choose a dark style for a dark terminal. NO_COLOR and editor.color: false turn the colors off.

hail reads config.yaml: the prompt, an alias and the style dracula. hail --list-styles lists the styles, and hail --preview-style dracula shows a sample in that style

The interactive shell

On a terminal, hail edits each line with rline, and reads the key bindings of your inputrc file. It shows PS1 and PS2 with the escapes of bash, and runs PROMPT_COMMAND and PS0. Tab completes commands, files and variables, and the arguments of a command that complete names, so the functions of bash-completion work. The history follows HISTFILE, HISTSIZE, HISTCONTROL, HISTIGNORE and HISTTIMEFORMAT, and !! and the other history references expand.

Up brings back the commands before, Ctrl-R finds a command by a part of it, and history lists the commands After set -o vi, Esc and the keys of vi change words of the line Ctrl-Z stops less, sleep runs in the background, jobs lists both, and fg brings less back

The keys of bash work, among them:

Keys What they do
Up, Down, Ctrl-R Go through the history, and search it
Tab Complete the word at the cursor
Esc /, Esc !, Esc $, Esc ~, Esc @ Complete a file, a command, a variable, a user or a host
Ctrl-X Ctrl-E Edit the line in $VISUAL or $EDITOR, and run it
Esc Ctrl-F, Esc Ctrl-B Move over a shell word, where a quoted string is one word

bind and set -o vi change the keys at once, and bind -x binds a key to a shell command that can read and change READLINE_LINE. A few functions of bash are not in hail yet, such as shell-expand-line and magic-space.

On Unix, hail has job control, as bash has. Ctrl-Z stops the command that runs, and jobs, fg, bg, disown, wait and kill take job specs such as %1 and %+. hail reports a job that ended or stopped before the next prompt.

Windows

hail runs natively on Windows. It finds a program by the extensions of PATHEXT, and runs a script with a #! line through the program that the line names, as Git for Windows does, so #!/usr/bin/env python3 runs python3.exe. It reads scripts with CRLF line ends. It shows paths as Git Bash does, as in /c/Users.

<(command) and >(command) give the path of a temporary file, because no program of Windows can open /dev/fd/N. Windows cannot stop a process, so Ctrl-Z, fg and bg do not work there. A build of TinyGo for Windows is not supported.

Related projects

Project What hail takes from it
xo/ox Command line parsing, configuration files, help and completion scripts
xo/rline Line editing, history and completion
xo/inputrc The reader of inputrc files, which rline uses
xo/transit A pure Go port of tree-sitter, with a bash grammar for highlighting
xo/terminfo The color depth of the terminal
xo/usql The peer whose conventions hail follows, such as config.yaml
reubeno/brush The parser, the interpreter and the tests that hail ports

Contributing

Report a bug, ask a question, or propose a feature in Discussions. CONTRIBUTING.md says how to build hail and run its tests. The design is in docs/DESIGN.md, where hail stands is in docs/PLAN.md, the known work is in docs/BACKLOG.md, and each decision is in docs/decisions.

License

hail is under the MIT license. The YAML test cases that come from brush keep the MIT notice of brush. No file of GNU bash is in this repository.

About

A Unix shell in pure Go that runs bash scripts and replaces bash at the terminal, with TinyGo and WebAssembly builds

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages