Skip to main content
⚡ OpenDevDocs
Shell
shellshell-scriptingGuideBeginner

Shell I/O Redirection (>, >>, <, 2>&1)

Redirect standard input, overwrite files with >, append with >>, capture stderr with 2>, and merge streams with 2>&1.

Verified: 2026-09-30
Tested on:bash 5.x
Platforms:
linuxmacos
#shell#bash#redirection#io#cli

Redirection Operators Reference

OperatorActionExample
>Redirect stdout, overwriting destination fileecho "Hello" > output.txt
>>Redirect stdout, appending to destination fileecho "New Line" >> logs.txt
<Redirect stdin from a filemysql -u root < dump.sql
2>Redirect stderr (errors only)ls /root 2> error.log
2>>Append stderr to a filebackup.sh 2>> backup_errors.log
2>&1Merge stderr into stdoutapp.sh > full.log 2>&1
&>Modern Bash shorthand for > file 2>&1app.sh &> full.log
<<<Here-string (passes string as stdin)grep "foo" <<< "foobar"

Practical Examples

1. Separate Standard Logs from Error Logs

python3 train_model.py > output.log 2> errors.log

2. Capture Both Outputs to Single File

# Classic POSIX syntax
./build.sh > build.log 2>&1

# Modern Bash shorthand
./build.sh &> build.log

3. Multi-Line Here-Documents (<< EOF)

cat << 'EOF' > /etc/nginx/conf.d/api.conf
server {
    listen 80;
    server_name api.example.com;
    location / {
        proxy_pass http://localhost:3000;
    }
}
EOF

On this page