~kris/dots

srice

ref: 9b4572416de8a234ffd5ebaaaa549a930b3e2041 srice/doc/rc-shell-reference.md -rw-r--r-- 2.4 KiB
9b457241 — Kris Yotam wip: emergency backup (power outage risk) 3 months ago

#rc shell reference

Quick reference for Plan 9 rc shell syntax. Extracted from plan9port.

#Variables

var=value            # assignment
var=(a b c)          # list
$var                 # expand
$#var                # count
$var(2)              # index (1-based)
$$var                # indirect
$"var                # stringify list (join with spaces)
$var^suffix          # concatenate

#Control Flow

if(test) cmd
if not cmd

for(i in list) cmd
for(i) cmd           # implicit $*

while(test) cmd

switch($var) {
case pattern
    cmd
case pat1 pat2
    cmd
}

#Functions

fn name { body }
fn sigexit { cleanup }    # trap on exit
fn sigint { }             # trap ctrl-c

#Pattern Matching

~ $var pattern           # true if matches
~ $var *.c *.h           # multiple patterns
! ~ $var pattern         # negation

#Redirection

> file                   # stdout
>> file                  # append
< file                   # stdin
>[2] file                # stderr
>[1=2]                   # merge stdout to stderr
>[2=1]                   # merge stderr to stdout
<{cmd}                   # process substitution (in)
>{cmd}                   # process substitution (out)

#Command Substitution

`{cmd}                   # split on $ifs
``(sep){cmd}             # split on sep

#Process Control

rfork e                  # new env (copy)
rfork en                 # new env + namespace
rfork ns                 # new namespace only

#Status

$status                  # exit status of last command
exit ''                  # success
exit 'error msg'         # failure

#Flags

flag p && ...            # test if -p flag was passed
getflags                 # parse flags

#Quoting

'literal'                # no expansion
''                       # empty string
''''                     # literal single quote
# no double quotes -- use ^ for concatenation

#Key Differences from sh/bash

- No double quotes (use $"var for stringify)
- path is a list, not colon-separated
- Lists are first class (not just strings)
- No export -- env is inherited via fork
- if/if not instead of if/else/elif
- ~ for pattern matching instead of [[ ]]
- fn instead of function
- $status instead of $?
- ^ for string concatenation
- No arithmetic expansion -- use expr or bc