concha is a Unix shell whose scripting language is Pascal. Commands
are commands, and everything around them is Pascal: real variables,
real conditions, real functions. There is no word splitting, no quote
maze and no [ ... ]. At the prompt it has a line editor of its own
that knows the language: it highlights as you type, suggests from
history, completes with a menu and says "did you mean".
| sh | concha |
|---|---|
for f in *.log; do
n=$(grep -c ERROR "$f")
if [ "$n" -gt 0 ]; then
echo "$f: $n errors"
fi
done |
for f in glob('*.log') do begin
n := int(`grep -c ERROR $f`)
if n > 0 then writeln(f, ': ', n, ' errors')
end |
$f is always one argument, even when the name has spaces in it, so
there is nothing to quote.
- It keeps what the Bourne shell is good at: commands, pipes,
redirections, heredocs,
&&,||,&, globs and job control. - It drops what makes shell scripts fragile: values split and
globbed again,
$IFS,testand[ ... ], and quoting to keep a value whole.
Concha is Spanish for a seashell, and a shell is what concha is. The
emblem is a conch tipped on its side, with a shell's prompt, >_,
waiting in its opening.
Above, concha at its prompt: a command highlighted as it is typed, a
capture and some arithmetic, a loop over glob typed on two lines (the
. prompt asks for the rest), a typo that gets "did you mean echo?"
and the fix offered, dim, as the next line, and Tab's menu completing a
file name. doc/demo.cast is the same session as an asciinema
recording (asciinema play doc/demo.cast). make cast makes both from
doc/demo.keys: tests/Play.raz types the keys into concha on a
pseudo-terminal at a person's pace, with the times counted from the
pauses rather than read from the clock, and tests/cast.cn draws the
GIF from the cast. The same keys give the same cast and the same GIF.
concha is written in raz, and builds
with the version the Makefile names (RAZVERSION); make warns if
the raz on your PATH is another. It needs
rzone, for local time, installed first
with make install in its checkout (it goes to ~/.raz/lib).
make # build/concha
make test # the self-test, the examples, the man pages
make install # concha, three man pages and the examples, into ~/.local
PREFIX, DESTDIR, BINDIR, MANDIR, DATADIR and RAZ can be set
on the command line; see the Makefile.
| command | does |
|---|---|
concha |
the prompt; reads ~/.concharc first |
concha script.cn a b |
runs a script, with a and b in args |
concha -c 'text' a b |
runs text |
concha -n script.cn |
checks the syntax and runs nothing |
concha -q kind script.cn |
answers an editor (below) |
... | concha |
runs standard input |
concha --version, concha -h |
the version, the usage |
man concha-tutorial is a tour for people who already use a shell,
with a table translating sh to concha; man concha is the whole
reference, and man concha-complete the completion files.
examples/ holds short commented scripts, and
examples/concharc a ~/.concharc to start from,
whose prompt counts the lines changed in a git or sauce tree. What
follows is the short of it.
A line that starts with a keyword, an assignment (x :=) or a call (f()
is Pascal. Any other line is a command.
ls -l /tmp | grep conf > out.txt // a command
n := length(lines(`ls`)) // Pascal
writeln('there are ', n, ' files')
A newline or ; ends a command. So does a bare end, else or until
that closes an open begin, if, case or repeat, so
if x > 1 then echo big else echo small works as written. Anywhere else
these are ordinary words: echo the end prints the end. Between Pascal
statements the ; is optional. Comments are // ... and { ... }.
| word | means |
|---|---|
foo |
itself; *, ? and [..] glob, and no match is an error |
'it''s' |
a Pascal string, taken literally |
"a $x b" |
$name, ${name} and $(expr) interpolated |
$x |
a variable's value: one argument, or one per list item |
$(expr) |
any expression |
`cmd` |
the command's output, its trailing newlines removed |
<(cmd) |
a /dev/fd/N to read the command's output from |
~ |
$HOME |
\c |
the character c |
Values are never re-split or re-globbed. To split, say so:
rm $(lines(`find . -name '*.o'`)).
Redirections are < f, > f, >> f, 2> f, 2>&1, &> f,
<<< 'text' and heredocs. A heredoc's body starts on the line after the
command; a quoted tag (<<'EOF') makes it literal, and an indented
terminator takes its indentation off every line of the body:
procedure usage;
begin
cat <<EOF
usage: $prog file
EOF
end;
Values are nil, booleans, integers, strings and lists ([1, 'two']),
and they are copied on assignment. + adds integers, joins lists, and
otherwise concatenates strings. -, *, div and mod read numeric
strings, so int(`wc -l < f`) + 1 works, and = treats '10' and
10 as equal. Conditions must be boolean. Operator precedence is
Pascal's, so write (a = 1) and (b = 2). Strings index from 1, lists
from 0.
var x := 1, y if c then s else s
x := e; l[i] := e while c do s
begin ... end repeat ... until c
for i := 1 to n do s for f in glob('*.txt') do s
for i := n downto 1 do s case e of 1, 2: s; 'x': s else ... end
export PATH := PATH + ':/opt/bin' break continue exit exit(e)
procedure greet(name); function sq(x);
begin begin
writeln('hello, ', name) result := x * x
end; end;
A procedure can be called as a command (greet bob) or as Pascal
(greet('bob')), and every call also gets its whole argument list in
args. New variables are local inside routines and global outside them,
and nothing outside a routine sees its locals. Only exported globals
reach child processes; the environment concha starts with is exported
already.
A begin ... end block, or any control statement, can be a stage of a
pipeline or take redirections:
printf 'a\nb\n' | while readln(l) do writeln('> ', l)
begin echo b; echo a end | sort > sorted.txt
Procedures and blocks run inside the shell, so they can change variables; in the middle of a pipeline they run in a child, and can't.
After each command, status holds its exit code, 128+n if signal n
killed it. Pascal statements leave it alone, but a call made as a
statement is a command: writeln(...) has status 0, and a procedure the
status of the last command it ran. Under errexit a failed command ends
the script with concha: f.cn:12: errexit: grep -q x (status 1).
grep -q root /etc/passwd
if status = 0 then writeln('yes')
make && ./prog || echo failed
errexit := true stops a script at the first command that fails, and
pipefail := true makes a pipeline fail when any of its stages does.
halt(n) ends the shell; exit leaves the routine or the script, and
exit(e) sets the function's result or the status as it goes.
- Commands:
cd [-],echo [-n],exec,source file,trap,ulimit,umask,unset,wait [pid], and for jobsjobs,fg,bganddisown. - Input and output:
write,writelnandreadln(v), which answers false at the end of input. Awriteused as a command writes to the command's standard output, sowriteln(x) > fileworks. - Strings and lists:
length copy pos int str chr ord upcase lowcase trim,unicode(s) chars(s) hex(n[, w])for UTF-8 and hex,split(s[, sep]) join(l[, sep]) lines(s) replace(s, old, new) sort(l),glob(pattern) match(s, pattern). - Files:
exists(p) isdir(p) isfile(p) basename(p[, suffix]) dirname(p). - The shell:
inc(v[, n]) dec(v[, n]) halt([n]) defined(name),environ() getenv(name) setenv(name, value),now() time(fmt[, ms])andcompletions(line).
- Editing has the usual keys: the arrows,
^A^E^B^F,Alt-bAlt-f,^WAlt-d^U^K,^Yto put back what was killed,^_to undo,Alt-.for the last word of the line before,^T,^L, and^X^Eto edit the line in$EDITOR. The man page lists them all. - Colour highlights the line as you type, and the command word is
blue when it can run and red when it can't.
NO_COLOR=1turns colour off. - History is kept in
~/.concha_history.UpandDownwalk it,^Rsearches it, and the newest line that starts with what you have typed is suggested, dim, after the cursor:Rightor^Etakes it. Afterehco hiand its "did you mean echo?", the next prompt suggestsecho hi. - Tab completes commands, procedures,
$variablesand file names, with a menu when several fit. Afunction complete(words)teaches it your commands' arguments, git branches say. A program can describe its subcommands, options and operands in a small file instead (concha-complete(5)), which Tab reads without ever running it.completions(line)lists what Tab would offer. - The prompt is a
procedure prompt, if you write one, and the default if it fails. Input that isn't finished yet (abeginwith noend, an open string, a heredoc) gets a.prompt for the rest. - Jobs: Ctrl-C interrupts the command or the loop that runs.
Ctrl-Z stops the command in the foreground;
jobslists the jobs,fgandbggo on with one in the foreground or the background, anddisownlets one outlive the terminal.
concha answers questions about a script without running it, one line a
thing and its fields split by tabs. They are what concha-lsp(1)
(~/lib/concha-lsp), concha's language server, asks to give an editor
errors, an outline, completion, hover and go to definition.
concha -q check script.cn the first syntax error: line, column, message
concha -q outline f.cn its procs, funcs and vars, and where they are
concha -q words f.cn:3:1 what can be typed there
concha -q def f.cn:3:1 where the routine named there is declared
Five ideas, a module each. Read them in this order; each module depends only on those before it.
- Values, not words (
src/Ccore.raz). A value isnil, a boolean, an integer, a string or a list, and it stays one value wherever it goes:$xis one argument, or one per item of a list, and nothing is split or globbed again. Scratch values live in an arena released after every statement, so a long loop runs in flat memory. An error is raz'sERROR, passed up withtry. - Two worlds in one scanner (
src/Csyntax.raz). A line that starts with a keyword,name :=orname(is Pascal; any other is a command, read word by word. One scanner switches between the two, so a capture can hold commands inside a Pascal expression and$(expr)can hold Pascal inside a command word. A script is parsed whole before it runs, and the parser keeps notes for the queries. - Names, patterns, time (
src/Cvars.raz,src/Cglob.raz,src/Cclock.raz). Variables in frames, procedures,PATHand the "did you mean" that names the one close misspelling; globbing; andtime()'s format, with local time from rzone. - Run what can run in the shell (
src/Cinterp.raz). Procedures, blocks and builtins run in the shell itself, so they can change its variables; only programs, pipeline stages and captures get a child. It does processes, pipes, redirections,<(...), job control, signals,errexitand the builtins. - An editor that knows the language (
src/Cfuzzy.raz,src/Ccomplete.raz,src/Cedit.raz). The line editor colours the line with the parser's own reading of it, so the colour is never wrong about what will run; Tab ranks completions with a fuzzy matcher, and reads concha-complete(5) files without running anything.src/Concha.razties it together: the command line, the prompt, startup and the-qqueries.
doc/internals.md goes deeper, for people changing concha.
tests/test.cnholds 370 checks, run bymake testunder the concha just built: the language, expansion, redirections, pipes, jobs, signals, traps,errexit, completion and the queries. Each builtin's usage is checked against the man page.make testalso parses every script inexamples/with-nand checks the three man pages withgroff -ww, which must give no warnings.- The build uses
raz -Werror, andmake fmtchecks thatsrc/is laid out as raz's formatter lays it out. - concha was first written in Free Pascal and ported to raz with its language kept as it was: the same suite passed under both, and the port was the shell of every terminal before the Pascal one was retired. The few differences that were meant are listed at the end of doc/internals.md.
About 9,700 lines of raz in ten modules: 8,100 of code and 960 of
comments, as rloc counts them (make loc). There is nothing under
it but raz's library: no libc.
- Word splitting,
$IFS,[andtest, arithmetic expansion, brace expansion andeval. Pascal has values, conditions and expressions, so these have nothing left to do. >(list)and subshell parentheses.<(list)reads a command's output as a file;begin ... endgroups commands.- Assignments before a command.
FOO=bar cmdis the commandFOO=bar; when it isn't found, concha suggestsenv FOO=bar cmd. - getpwnam(3). With no libc,
~useris looked up in/etc/passwdonly, so users from LDAP and the like are not known. - POSIX sh compatibility. concha doesn't run sh scripts;
SHELLstays/bin/bashinside it, so programs that run$SHELL -cstill get sh.
Defect reports are welcome; CONTRIBUTING.md says how.
ISC. See LICENSE.
