Redirections (> >> 2>)
In this page:
Redirections (> >> 2>)
> redirects standard output to a file, overwriting it; >> appends instead of overwriting. 2> redirects standard error separately from standard output, and 2>&1 merges error into the same stream as output. Redirection is essential for logging and separating normal output from error messages.
Warning: > truncates the target file immediately, even if the command that follows fails to run.
Example: Redirections (> >> 2>)
#!/bin/bash
echo "First line" > output.txt
echo "Second line" >> output.txt
cat output.txt
ls /no/such/path 2> error.txt
cat error.txt
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: