redirection
Sending a command's output into a file instead of the terminal: > replaces the file, >> appends to the end of it.
In more detail
> replaces the file and >> appends to it. 2>&1 sends error output to the same place as normal output, and >&2 sends a message to standard error. Appending has no memory of what the file already holds, so running the same line twice leaves two copies.
Examples
echo "listen 8080" >> by-hand/server.conf appends a second copy on a second run; the lineinfile module adds the line only if it is missing.noisy > out.txt 2> err.txtcmd >> log 2>&1printf 'service.env\n' > .gitignoreRelated terms
- echo — The shell command that prints its arguments as a line of text.
- shell script — A text file of shell commands that runs them in order, top to bottom, as if you had typed them one by one.
- lineinfile module — The module that makes sure one particular line is present in, or absent from, a text file.
- standard output and standard error — The two channels a program writes normal output (stdout) and error messages (stderr) to, plus stdin for input.
- file descriptor — A small number the operating system gives a process for each open file, socket or pipe, used in every later read or write.
- /dev/null — A special file that throws away anything written to it and reads as empty.
- pipe — The | operator, which connects one command's standard output to the next command's standard input so they run together.
Taught in
- What Configuration Management Is — Ansible Configuration Management at Scale
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Standard Input, Output And Error — Bash Scripting & Command Line Automation
- Redirection: Truncate, Append, Both Streams — Bash Scripting & Command Line Automation
- Pipelines, tee And Process Substitution — Bash Scripting & Command Line Automation
- Finding Lines: grep And Regular Expressions — Bash Scripting & Command Line Automation
- Reshaping Text: cut, sort, uniq, tr, sed, awk — Bash Scripting & Command Line Automation
- Command Substitution And Subshells — Bash Scripting & Command Line Automation
- Script Input And Output: read, Heredocs, printf — Bash Scripting & Command Line Automation
- Debugging: set -x, bash -n And Reading The Error — Bash Scripting & Command Line Automation
- Archives, Transfers And The Network — Bash Scripting & Command Line Automation
- Scheduling, Monitoring And A Script That Reports — Bash Scripting & Command Line Automation
- Conventional Commits — Advanced Git Internals & Distributed Workflows