stdin stdout stderr
| Stream | Abbreviation | Number |
|---|---|---|
| Standard input | stdin | 0 |
| Standard output | stdout | 1 |
| Standard error, or error stream | stderr | 2 |
Redirecting Standard Output and Standard Error
| Redirection Operator | Description | Example |
|---|---|---|
| > | Redirecting stdout, then overwrite | ls -l /usr/bin > ls-output.txt> ls-output.txt 清空文件 |
| >> | Redirecting stdout, then append | ls -l /usr/bin >> ls-output.txt |
| 2> | Redirecting stderr, then overwrite | ls -l /usr/bin 2> ls-error.txt |
| 2>> | Redirecting stderr, then append | ls -l /usr/bin 2>> ls-error.txt |
| &> | Redirecting both stdout and stderr, then overwrite | ls -l /bin/usr &> ls-output.txt |
| &>> | Redirecting both stdout and stderr, then append | ls -l /bin/usr &>> ls-output.txt |
| 2>&1 | Same as &>, but the traditional way, which works with old versions of the shell | ls -l /bin/usr > ls-output.txt 2>&1 |
| >&2 1>&2 |
Same as &>, but the traditional way, which works with old versions of the shell; | ls -l /bin/usr > ls-output.txt >&2echo "Invalid: $PARAM" >&2 |
/dev/null
$ ls -l /bin/usr 2> /dev/null
Redirecting Standard Input
| Redirection Operator | Description | Example |
|---|---|---|
| < | Redirecting stdin | cat > file.txt , see also cat |
Pipelines
| pipeline operator
$ ls -l /usr/bin | less$ ls /bin /usr/bin | sort | less
Differences between redirection operator and pipeline operator
- The redirection operator connects a command with a file
- while the pipeline operator connects the output of one command with the input of a second command.
Here Documents:
Also called here script ```bash command << token text token<<<<-
$ cat << EOF
some text some text more EOF some text some text more
``<<-`: the shell will ignore leading tab characters (but not spaces) in the here document.
Here String: <<<
A here string is like a here document, only shorter, consisting of a single string.
$ file_info=$(grep "^$USER:" /etc/passwd)$ IFS=":" read user pw uid gid name home shell <<< "$file_info"
