-
Notifications
You must be signed in to change notification settings - Fork 4
Command Chaining
Albert Álef edited this page Feb 3, 2026
·
2 revisions
RubyShell supports shell operators for building pipelines and command chains.
Chain commands using the pipe operator:
sh do
chain { cat("file.txt") | grep("error") | wc("-l") }
endCommands ending with ! return a Command object without executing. This allows building chains:
sh do
# Build a pipeline
result = (cat!("data.csv") | sort! | uniq!).exec
# Or step by step
cmd = cat!("file.txt")
cmd = cmd | grep!("pattern")
cmd.exec
endsh do
chain { echo("hello") > "output.txt" }
endsh do
chain { echo("another line") >> "output.txt" }
endPass input to a command:
# From a string
sh.wc("-l", _stdin: "line1\nline2\nline3")
# From another command's output
output = sh.cat("source.txt")
sh.grep("pattern", _stdin: output)Run commands in the background:
sh do
chain { sleep("10") & }
endExecute second command only if first succeeds:
sh do
chain { mkdir("test") && cd("test") }
endThe chain block provides a context for building command chains:
sh do
result = chain do
cat("access.log") | grep("404") | sort | uniq("-c") | sort("-rn") | head("-10")
end
puts result
endNext: Error Handling