AnyCompiler

Command Substitution Aur Pipes

| ek command ka output directly next command ke input mein bhej deta hai, bina kisi intermediate variable ke — isi tarah Bash chhoti, single-purpose commands ko ek bare pipeline mein compose karta hai.

echo -e "banana\napple\ncherry" | sort
Output
apple
banana
cherry

Puri pipeline ko $(...) mein wrap karna us ka final output ek variable mein capture kar leta hai, quoting lesson wali hi command-substitution syntax — yeh ek pipeline par exactly waise kaam karta hai jaise ek single command par karta hai.

sorted=$(echo -e "banana\napple\ncherry" | sort)
echo "$sorted"
Output
apple
banana
cherry

Aage: redirection and heredocs — output ko file mein bhejna ya discard karna, aur multi-line text inline likhna.

Practice exercise

Lines "banana", "apple", "cherry" (echo -e se, ek line par ek) ko sort se pipe karein, sorted result ko command substitution se ek variable mein capture karein, aur woh variable print karein.