Files
lush/issues/07-redirection.md
Cormac Shannon db6f39a2a4 Add project issues for lush (Lua + shell) features
Defines syntax decisions and implementation plan across 7 issues:
- backtick command execution returning {code, stdout, stderr}
- ${expr} interpolation in backticks
- $SIGIL env var read/write
- argv parsing, piping, and redirection (future)
2026-02-18 19:08:59 +00:00

29 lines
1007 B
Markdown

# 07 — Implement I/O redirection for commands
**Status:** open (post-core)
**Blocked by:** #03
## Syntax
```lua
`ls -l` > "output.txt" -- redirect stdout to file
`ls -l` >> "output.txt" -- append stdout to file
`cmd` 2> "err.txt" -- redirect stderr to file
`cmd` 2>&1 -- merge stderr into stdout
`cmd` < "input.txt" -- redirect stdin from file
```
## Implementation
- Before `execvp()` in the child process, use `dup2()` to redirect file descriptors
- Parse redirection operators as part of the command syntax
- Open target files with appropriate flags:
- `>``O_WRONLY | O_CREAT | O_TRUNC`
- `>>``O_WRONLY | O_CREAT | O_APPEND`
- `<``O_RDONLY`
- `2>&1``dup2(stdout_fd, STDERR_FILENO)`
## Challenge
`>` and `>>` conflict with Lua's greater-than and right-shift operators. Like piping, these operators must only be valid in command context. The parser can disambiguate because the left-hand side is a command expression.