Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators & Redirection

Hash shell supports Unix operators for connecting and controlling commands.

Overview

OperatorTypeDescription
|PipeConnect stdout to stdin
>RedirectOutput to file (overwrite)
>>RedirectOutput to file (append)
<RedirectInput from file
2>RedirectStderr to file
&>RedirectBoth stdout and stderr
&&ChainRun if previous succeeded
||ChainRun if previous failed
;ChainRun sequentially
&BackgroundRun in background

Quick Examples

Pipes

ls | grep txt
cat file.txt | sort | uniq
ps aux | grep nginx | wc -l

Redirection

echo "Hello" > file.txt       # Overwrite
echo "World" >> file.txt      # Append
cat < input.txt               # Input from file
command 2> errors.log         # Stderr
command &> all.log            # Both

Command Chaining

make && make install          # Run if success
cd /nonexistent || echo "Failed"  # Run if fail
echo "Start"; command; echo "End" # Sequential

Background

long_command &                # Run in background

Learn More