Redirections allow you to control where command input comes from and where output goes.
Overview
Minishell supports four types of redirections:
| Operator | Name | Description |
|---|
< | Input redirection | Read input from file |
> | Output redirection | Write output to file (overwrite) |
>> | Append redirection | Append output to file |
<< | Heredoc | Read input until delimiter |
All redirections are processed before command execution, in left-to-right order.
Syntax
Description
Reads command input from a file instead of standard input (keyboard).
Examples
# Count lines in a file
wc -l < file.txt
# Sort file contents
sort < unsorted.txt
# Read and display file
cat < document.txt
# Multiple commands with input redirection
grep "error" < logfile.txt | wc -l
Error Handling
# File doesn't exist
cat < nonexistent.txt
# Output: nonexistent.txt : No such file or directory
# Exit status: 1
# Permission denied
cat < /root/secret.txt
# Output: /root/secret.txt : Permission denied
# Exit status: 1
If the input file cannot be opened, the command will not execute and an error message is displayed.
Output Redirection (>)
Syntax
Description
Redirects command output to a file, overwriting the file if it exists.
Examples
# Save command output
ls -la > directory_listing.txt
# Create or overwrite file
echo "Hello World" > greeting.txt
# Save error output (stderr not redirected in basic >)
ls /nonexistent > errors.txt
# errors.txt will be empty; error appears on screen
# Empty a file
> empty_file.txt
echo "" > empty_file.txt
File Creation
# Creates file with permissions 0666 (rw-rw-rw-)
echo "content" > newfile.txt
# Overwrites existing file
echo "first" > file.txt
echo "second" > file.txt
cat file.txt
# Output: second
Output redirection with > overwrites the target file completely. Use >> to append instead.
Append Redirection (>>)
Syntax
Description
Appends command output to a file without overwriting existing content.
Examples
# Append to log file
echo "[INFO] Application started" >> app.log
echo "[INFO] Loading configuration" >> app.log
# Build file incrementally
echo "First line" >> document.txt
echo "Second line" >> document.txt
echo "Third line" >> document.txt
cat document.txt
# Output:
# First line
# Second line
# Third line
# Append command output
ls -la >> inventory.txt
date >> inventory.txt
File Creation
# Creates file if it doesn't exist
echo "New content" >> newfile.txt
# Appends if file exists
echo "More content" >> newfile.txt
Append redirection creates the file with permissions 0666 if it doesn’t exist, just like output redirection.
Heredoc (<<)
Syntax
command << DELIMITER
content line 1
content line 2
DELIMITER
Description
Reads input from the command line until a delimiter is encountered. Useful for multi-line input.
Examples
# Basic heredoc
cat << EOF
This is line 1
This is line 2
This is line 3
EOF
# Output:
# This is line 1
# This is line 2
# This is line 3
# Common delimiters (any word works)
cat << END
Content here
END
cat << STOP
More content
STOP
# Heredoc with grep
grep "error" << EOF
info: starting
error: failed to connect
warning: retrying
error: timeout
EOF
# Output:
# error: failed to connect
# error: timeout
# Create multi-line file
cat << EOF > config.txt
server=localhost
port=8080
timeout=30
EOF
# Heredoc with variable expansion
cat << EOF
User: $USER
Home: $HOME
EOF
# Output:
# User: john
# Home: /home/john
Interactive Prompt
When using heredoc, Minishell displays a > prompt for each line:
cat << END
> First line
> Second line
> Third line
> END
EOF Handling
If you press Ctrl+D (EOF) before entering the delimiter, Minishell displays a warning:cat << EOF
> some text
> ^D
warning: here-document delimited by end-of-file (wanted `EOF')
Combining Multiple Redirections
# Last redirection takes precedence
cat < file1.txt < file2.txt < file3.txt
# Reads from file3.txt only
Multiple Output Redirections
# Last redirection takes precedence
echo "Hello" > file1.txt > file2.txt
# Writes to file2.txt only
# Read from input.txt, write to output.txt
sort < input.txt > output.txt
# Same with append
grep "error" < logfile.txt >> errors.txt
# Heredoc to file
cat << EOF > config.txt
key=value
another=setting
EOF
Order Independence
# These are equivalent
command < input.txt > output.txt
command > output.txt < input.txt
# Redirections can be anywhere
< input.txt command > output.txt
Redirections with Pipes
Redirections work seamlessly with pipes:
# Output redirection after pipe
cat file.txt | grep "error" > errors.txt
# Input redirection before pipe
< input.txt sort | uniq > output.txt
# Multiple pipes and redirections
< data.txt grep "keyword" | sort | uniq > results.txt
# Heredoc with pipe
cat << EOF | grep "error"
info: started
error: failed
warning: slow
EOF
When combining pipes and redirections, redirections affect only the specific command they’re attached to, not the entire pipeline.
Error Handling
Invalid Files
# Input file doesn't exist
cat < missing.txt
# Output: missing.txt : No such file or directory
# Exit status: 1
# Cannot create output file
echo "test" > /root/file.txt
# Output: : No such file or directory
# Exit status: 1
Syntax Errors
# Missing filename
cat <
# Output: syntax error: redir
# Multiple redirections without file
cat < < file.txt
# Output: syntax error: redir
# Invalid combinations
cat <> file.txt
# Output: syntax error: redir
Syntax errors in redirections prevent command execution entirely. Make sure each redirection operator is followed by a valid filename.
Advanced Examples
Combining All Types
# Complex pipeline with redirections
< input.txt grep "error" | sort >> sorted_errors.txt
# Heredoc with multiple commands
cat << EOF | grep "user" | sort > users.txt
user: john
admin: root
user: jane
guest: visitor
EOF
Creating Multi-file Processing
# Process file and save results
sort < unsorted.txt > sorted.txt
uniq < sorted.txt > unique.txt
wc -l < unique.txt > count.txt
Log Accumulation
# Build log file over time
date >> application.log
echo "Starting service" >> application.log
ls -la >> application.log
echo "Service started" >> application.log
Implementation Details
For developers interested in the internals:
- Redirection parsing: set_infile_outfile.c:93-120
- Input file handling: set_infile_outfile.c:68-91
- Output file creation: set_infile_outfile.c:40-52 (
>)
- Append file handling: set_infile_outfile.c:54-66 (
>>)
- Heredoc implementation: set_infile_outfile.c:15-38
- File descriptors: STDIN_FILENO (0), STDOUT_FILENO (1)
- Temporary heredoc file:
.\vtemp\th