# 输入输出重定向

想要把多个Linux 命令适当地组合到一起，使其协同工作，以便我们更加高效地处理数据。要做到这一点，就必须搞明白命令的输入重定向和输出重定向的原理。
简而言之，输入重定向是指把文件或者键盘输入导入到命令中，而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。在日常的学习和工作中，相较于输入重定向，我们使用输出重定向的频率更高，所以又将输出重定向分为了标准输出重定向错误输出重定向两种不同的技术，以及清空写入追加写入两种模式。听起来就很玄妙？

• 标准输入重定向（STDIN，文件描述符为0）：默认从键盘输入，也可从其他文件或命令中输入。
• 标准输出重定向（STDOUT，文件描述符为1）：默认输出到屏幕。
• 错误输出重定向（STDERR，文件描述符为2）：默认输出到屏幕。

对于输入重定向来讲，用到的符号及其作用如下表所示：

对于输出重定向来讲，用到的符号及其作用如下表所示：

[root@lynchj tmp]# man bash > readme.txt
BASH(1)                                                                              General Commands Manual                                                                             BASH(1)

NAME
bash - GNU Bourne-Again SHell

SYNOPSIS
bash [options] [file]

Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.

DESCRIPTION
Bash  is  an  sh-compatible  command language interpreter that executes commands read from the standard input or from a file.  Bash also incorporates useful features from the Korn and C
shells (ksh and csh).

Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1).  Bash can be configured to be POSIX-confor‐
mant by default.

OPTIONS
All  of  the  single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked.  In addition, bash interprets the
following options when it is invoked:

-c string If the -c option is present, then commands are read from string.  If there are arguments after the string, they are assigned to the positional parameters, starting with $0. -i If the -i option is present, the shell is interactive. -l Make bash act as if it had been invoked as a login shell (see INVOCATION below). -r If the -r option is present, the shell becomes restricted (see RESTRICTED SHELL below). -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell. -D A list of all double-quoted strings preceded by$ is printed on the standard output.  These are the strings that are subject to language translation when the current locale is
not C or POSIX.  This implies the -n option; no commands will be executed.
[-+]O [shopt_option]
shopt_option is one of the shell options accepted by the shopt builtin (see SHELL BUILTIN COMMANDS below).  If shopt_option is present, -O sets the value of  that  option;  +O
unsets  it.   If shopt_option is not supplied, the names and values of the shell options accepted by shopt are printed on the standard output.  If the invocation option is +O,
the output is displayed in a format that may be reused as input.
--        A -- signals the end of options and disables further option processing.  Any arguments after the -- are treated as filenames and arguments.  An argument of - is equivalent  to
--.

Bash also interprets a number of multi-character options.  These options must appear on the command line before the single-character options to be recognized.

--debugger
Arrange  for  the  debugger  profile  to  be  executed before the shell starts.  Turns on extended debugging mode (see the description of the extdebug option to the shopt builtin
below).
--dump-po-strings
Equivalent to -D, but the output is in the GNU gettext po (portable object) file format.
--dump-strings
Equivalent to -D.
--help Display a usage message on standard output and exit successfully.
--init-file file
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).

Equivalent to -l.

--noediting
Do not use the GNU readline library to read command lines when the shell is interactive.
………………省略部分输出信息………………

[root@lynchj tmp]# echo "哈哈，进行追加输出了" >> readme.txt
[root@lynchj tmp]# tail -n 10 readme.txt
cutes the next command in the sequence.  It suffices to place the sequence of commands between parentheses to force it into a subshell, which may be stopped as a unit.

Array variables may not (yet) be exported.

There may be only one active coprocess at a time.

GNU Bash-4.2                                                                            2010 December 28                                                                                 BASH(1)

-rw-r--r--. 1 root root 284185 May 15 16:53 readme.txt
ls: cannot access readme2.txt: No such file or directory

如果想把命令的报错信息写入到文件，该怎么操作呢？当用户在执行一个自动化的Shell脚本时，这个操作会特别有用，而且特别实用，因为它可以把整个脚本执行过程中的报错信息都记录到文件中，便于安装后的排错工作。接下来我们以一个不存在的文件进行实验演示：

[root@lynchj tmp]# ll xxxxxxxxxxxx
ls: cannot access xxxxxxxxxxxx: No such file or directory
[root@lynchj tmp]# ll xxxxxxxxxxxx > readme.txt
ls: cannot access xxxxxxxxxxxx: No such file or directory
[root@lynchj tmp]# ll xxxxxxxxxxxx 2> readme.txt
ls: cannot access xxxxxxxxxxxx: No such file or directory

[root@lynchj tmp]# wc -m < readme.txt
58

• 擅长领域：
• Java
• Linux

• 评论

• 上一篇
• 下一篇