Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2

- $(...) runs in a subshell. A word that is only an unquoted $(...) is
  one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 09:50:15 +01:00
parent f54ceb35af
commit f8c4ad64bc
13 changed files with 680 additions and 128 deletions

View file

@ -70,8 +70,40 @@
(expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1")
(expect "2>&1 before >" (c (str "ls 2>&1 >" file))
"err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2")
(expect ">&2 is not supported yet" (c "printf x >&2")
"err: bjosh: >&2 is not supported yet\ncode: 2")
(expect ">&2" (c "printf x >&2; echo y") "y\nerr: x")
(expect ">&2 with 2>" (c "printf x >&2 2>f")
"err: bjosh: >&2 with >, >>, 2>, 2>> or 2>&1 is not supported\ncode: 2")
;; $(...).
(expect "$(...)" (c "echo a$(echo b)c") "abc")
(expect "a $(...) word is one word per line" (c "printf '[%s]' $(printf 'a b\\nc\\n\\n')") "[a b][c]")
(expect "in double quotes, one word" (c "printf '[%s]' \"$(printf 'a\\nb\\n')\"") "[a\nb]")
(expect "nested" (c "echo $(echo $(echo in))") "in")
(expect "value commands write lines" (c "echo $(printf 'b\\na\\n' | so)") "a b")
(expect "shell commands" (c "cd /tmp; echo $(pwd)") "/tmp")
(expect "cd in it is undone" (c "cd /tmp; echo $(cd /; pwd); pwd") "/\n/tmp")
(expect "variables in it are its own" (c "X=1; echo $(X=2; echo $X) $X") "2 1")
(expect "exit ends only it" (c "echo $(exit 3; echo no)x") "x")
(expect "an assignment's status is its $(...)'s" (c "X=$(exit 4) || echo $?") "4")
(expect "a redirection in it" (c (str "echo [$(echo hidden >" file ")]; cat " file)) "[]\nhidden")
(expect "its stderr is not captured" (c "echo [$(echo e >&2)]") "[]\nerr: e")
(expect "2>&1 in it is" (c "echo [$(ls /no/such/dir 2>&1 | grep -c such)]") "[1]")
;; Aliases and command.
(expect "an alias" (c "alias say='echo said'; say it") "said it")
(expect "an alias with a pipe" (c "alias up='tr a-z A-Z'; echo x | up") "X")
(expect "an alias in an alias" (c "alias a1='echo one'; alias a2='a1 two'; a2 three") "one two three")
(expect "an alias of its own name" (c "alias ls='ls -d'; ls /") "/")
(expect "a quoted name is no alias" (c "alias say='echo said'; 'say' x") "err: bjosh: say: command not found\ncode: 127")
(expect "alias lists, sorted and quoted" (c "alias b='x y'; alias a=\"echo \\\"it's\\\"\"; alias")
"alias a='echo \"it'\\''s\"'\nalias b='x y'")
(expect "and runs that alias" (c "alias a=\"echo \\\"it's\\\"\"; a") "it's")
(expect "an alias must be one pipe" (c "alias x='a; b'")
"err: bjosh: alias: x: not one pipe: ;, &&, || and & are not allowed\ncode: 1")
(expect "unalias" (c "alias x=ls; unalias x; type x") "err: bjosh: type: x: not found\ncode: 1")
(expect "type of an alias" (c "alias x='ls -l'; type x") "x is an alias for ls -l")
(expect "command runs the program" (c "alias echo='echo E'; command echo x") "x")
(expect "command skips the value command" (c "printf 'a\\na\\n' | command uniq | wc -l") "1")
;; Shell commands.
(expect "cd and pwd" (c "cd /tmp; pwd") "/tmp")