### subst_simple
# Simple command substitution
echo $(echo hello)
### expect
hello
### end

### subst_in_string
# Command substitution in string
echo "result: $(echo 42)"
### expect
result: 42
### end

### subst_pipeline
# Command substitution with pipeline
echo $(echo hello | cat)
### expect
hello
### end

### subst_assign
# Assign command substitution to variable
VAR=$(echo test); echo $VAR
### expect
test
### end

### subst_nested
# Nested command substitution
echo $(echo $(echo deep))
### expect
deep
### end

### subst_multiline
# Multi-line output
echo "$(printf 'a\nb\nc')"
### expect
a
b
c
### end

### subst_with_args
# Command with arguments
echo $(printf '%s %s' hello world)
### expect
hello world
### end

### subst_arithmetic
# Command in arithmetic context
X=$(echo 5); echo $((X + 3))
### expect
8
### end

### subst_in_condition
# Command substitution in condition
if [ "$(echo yes)" = "yes" ]; then echo matched; fi
### expect
matched
### end

### subst_exit_code
# Exit code from command substitution
result=$(false); echo $?
### expect
1
### end

### subst_backtick
echo `echo hello`
### expect
hello
### end

### subst_multiple
# Multiple substitutions
echo $(echo a) $(echo b) $(echo c)
### expect
a b c
### end

### subst_with_variable
# Substitution using variable
NAME=test; echo $(echo $NAME)
### expect
test
### end

### subst_strip_trailing_newlines
# Command substitution strips trailing newlines
VAR=$(printf 'hello\n\n\n'); echo "x${VAR}y"
### expect
xhelloy
### end

### subst_nested_quotes
# Nested double quotes inside $() inside double quotes
echo "$(echo "hello world")"
### expect
hello world
### end

### subst_nested_quotes_var
# Variable expansion in nested quoted $()
x="John"; echo "Hello, $(echo "$x")!"
### expect
Hello, John!
### end

### subst_deeply_nested_quotes
# Deeply nested $() with quotes
echo "nested: $(echo "$(echo "deep")")"
### expect
nested: deep
### end

### subst_nested_single_quotes
# Single quotes inside $() inside double quotes
echo "$(echo 'single quoted')"
### expect
single quoted
### end

### subst_nested_quotes_no_expand
# Nested quotes without variable (literal string)
echo "result=$(echo "done")"
### expect
result=done
### end

### subst_nested_quotes_empty
# Nested quotes with empty inner string
echo "x$(echo "")y"
### expect
xy
### end

### subst_nested_quotes_multiple
# Multiple nested $() in same double-quoted string
echo "$(echo "a") and $(echo "b")"
### expect
a and b
### end

### subst_nested_quotes_escape
# Escaped characters inside nested $()
echo "$(echo "hello\"world")"
### expect
hello"world
### end

### subst_word_split_for_loop
# Command substitution output is word-split in for-loop list context
count=0
for f in $(printf '/src/one.txt\n/src/two.txt\n/src/three.txt\n'); do
  count=$((count + 1))
done
echo "$count"
### expect
3
### end

### subst_word_split_echo_multiword
# Command substitution producing space-separated words splits in for-loop
result=""
for w in $(echo "alpha beta gamma"); do
  result="${result}[${w}]"
done
echo "$result"
### expect
[alpha][beta][gamma]
### end

### subst_word_split_newlines
# Command substitution with newline-separated output splits on newlines
result=""
for line in $(printf 'x\ny\nz'); do
  result="${result}(${line})"
done
echo "$result"
### expect
(x)(y)(z)
### end

### subst_exit_trap_captured
# EXIT trap output should be captured inside $(), not leak to parent
result="$(trap 'echo TRAPPED' EXIT; echo hello)"
echo "captured=[${result}]"
### expect
captured=[hello
TRAPPED]
### end

### subst_exit_trap_with_explicit_exit
# EXIT trap fires on explicit exit inside $()
result="$(trap 'echo CLEANUP' EXIT; echo data; exit 0)"
echo "captured=[${result}]"
### expect
captured=[data
CLEANUP]
### end

### subst_exit_trap_no_leak
# Trap output must not leak to parent stdout
out="$(trap 'echo INSIDE' EXIT; echo body)"
echo "out=[${out}]"
### expect
out=[body
INSIDE]
### end

### subst_exit_trap_isolation
# EXIT trap in $() should not affect parent traps
trap 'echo PARENT' EXIT
result="$(trap 'echo CHILD' EXIT; echo inner)"
echo "result=[${result}]"
trap - EXIT
### expect
result=[inner
CHILD]
### end

### subst_function_isolation
# Functions defined in $() should not leak to parent shell
x=$(function foo() { echo "sub"; }; echo "ok")
echo "$x"
foo 2>/dev/null
echo "exit=$?"
### expect
ok
exit=127
### end

### subst_variable_isolation
# Variables set in $() should not leak to parent shell
myvar="before"
x=$(myvar="inside"; echo "ok")
echo "$x"
echo "$myvar"
### expect
ok
before
### end

### subst_alias_isolation
# Aliases set in $() should not leak to parent shell
x=$(alias myalias='echo aliased' 2>/dev/null; echo "ok")
echo "$x"
### expect
ok
### end
