In Linux, Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr) are fundamental concepts that allow processes to interact with the terminal or files. These streams are managed using file descriptors and can be redirected to/from files or other processes to control the flow of data effectively.
File Descriptors
- Standard Input (stdin):
- File Descriptor: 0
- Receives input from the keyboard or a file.
- Default source: Keyboard.
- Standard Output (stdout):
- File Descriptor: 1
- Sends output to the terminal or a file.
- Default destination: Screen.
- Standard Error (stderr):
- File Descriptor: 2
- Sends error messages to the terminal or a file.
- Default destination: Screen (separate from stdout).
Redirection
Redirection allows you to change the default source (stdin) or destination (stdout, stderr) of data.
1. Redirecting Standard Output
To redirect stdout to a file:
command > file
- Example:
ls > output.txt
- Writes the output of ls to output.txt (overwrites if the file exists).
To append stdout to a file:
command >> file
- Example:
echo “Additional line” >> output.txt
- Appends “Additional line” to output.txt.
2. Redirecting Standard Input
To redirect stdin from a file:
command < file
- Example:
wc < input.txt
- Reads the content of input.txt as input for the wc command.
3. Redirecting Standard Error
To redirect stderr to a file:
command 2> file
- Example:
ls non_existent_file 2> error.log
- Writes the error message to error.log.
To append stderr to a file:
command 2>> file
- Example:
ls non_existent_file 2>> error.log
- Appends the error message to error.log.
4. Redirecting Both stdout and stderr
To redirect both stdout and stderr to the same file:
command > file 2>&1
- Example:
ls non_existent_file > output.log 2>&1
- Writes both standard output and error messages to output.log.
Shortcut in Bash:
command &> file
- Example:
ls non_existent_file &> output.log
5. Using /dev/null
To discard output or errors, redirect them to /dev/null:
command > /dev/null 2>&1
- Example:
find / -name file.txt > /dev/null 2>&1
- Silently runs the command by discarding all output and errors.
Pipes
Pipes (|) redirect the output of one command as input to another command.
command1 | command2
- Example:
ls -l | grep “txt”
- Passes the output of ls -l to grep to filter lines containing “txt”.
Here Documents
A Here Document (<<) allows multi-line input directly in a script or command.
command << delimiter
text
delimiter
- Example:
cat << EOF
This is a sample text
for demonstration.
EOF
- Sends the multi-line input to the cat command.
Practical Examples
- Save Command Output to a File
ps aux > processes.txt
- Log Errors Separately
make 2> errors.log
- Filter Output Using Pipes
ps aux | grep “apache”
- Silence Command Output
wget http://example.com > /dev/null 2>&1
- Combining stdout and stderr
gcc program.c > output.log 2>&1
- Count Words in a File Using stdin
wc -w < file.txt
- Interactive Script with Here Document
ftp << EOF
open ftp.example.com
user username password
put file.txt
quit
EOF
Best Practices
- Always handle errors explicitly by redirecting stderr to a log file for troubleshooting.
- Use /dev/null to discard unwanted output and keep scripts clean.
- Chain commands with pipes to create efficient workflows.
- Use Here Documents for automating interactive commands like ftp or mysql.
Conclusion
Standard Input/Output and Redirection are essential features in Linux that enable effective command-line interaction and automation. By mastering these techniques, you can efficiently control data flow, handle errors, and build powerful scripts for a wide range of tasks.