Implement $(cmd) subcommand syntax in commands (issue #27)

Add $() for inline subcommand substitution that runs a command and
inserts its stdout (trailing newlines stripped) into the outer command
string. Supports nesting, and mixing with ${expr} and $NAME.
This commit is contained in:
Cormac Shannon
2026-03-18 09:29:51 +00:00
parent 42baabde34
commit 1766e40a68
8 changed files with 143 additions and 7 deletions

View File

@@ -109,4 +109,54 @@ do
assert(r.stdout == "$HOME\n", "got: " .. r.stdout)
end
-- $(cmd) subcommand: basic
do
local r = `echo $(echo hello)`
assert(r.stdout == "hello\n", "got: " .. r.stdout)
end
-- $(cmd) strips trailing newline from subcommand output
do
local r = `echo -$(echo world)-`
assert(r.stdout == "-world-\n", "got: " .. r.stdout)
end
-- $(cmd) multiple subcommands
do
local r = `echo $(echo aaa) $(echo bbb)`
assert(r.stdout == "aaa bbb\n", "got: " .. r.stdout)
end
-- $(cmd) nested subcommand
do
local r = `echo $(echo $(echo deep))`
assert(r.stdout == "deep\n", "got: " .. r.stdout)
end
-- $(cmd) with ${expr} inside subcommand
do
local r = `echo $(echo ${1+1})`
assert(r.stdout == "2\n", "got: " .. r.stdout)
end
-- $(cmd) with $NAME inside subcommand
do
local r = `echo $(echo $USER)`
assert(r.stdout == $USER .. "\n", "got: " .. r.stdout)
end
-- $(cmd) mixed with $NAME and ${expr}
do
local x = 42
local r = `echo $(echo sub) $USER ${x}`
local expected = "sub " .. $USER .. " 42\n"
assert(r.stdout == expected, "got: " .. r.stdout)
end
-- escaped \$(cmd) remains literal
do
local r = `echo \$(pwd)`
assert(r.stdout == "$(pwd)\n", "got: " .. r.stdout)
end
print "OK"