Skip to content
Justice Tech School

Command Substitution

$(command) runs a command and inserts its output. It nests and is clearer than backticks.

Example
count=$(printf "a\nb\nc\n" | wc -l)
echo "lines: $count"
upper=$(echo "hello" | tr a-z A-Z)
echo "$upper"
echo "nested: $(echo $(echo deep))"

Result

lines: 3
HELLO
nested: deep

Exercise

Capture output.

x=(date)

Quiz Test

What does $(cmd) give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.