“Bash Reference Manual/zh”的版本间的差异


跳转至: 导航, 搜索
Shell Syntax
Shell Operation
第312行: 第312行:
===Shell Operation===
===Shell Operation===
The following is a brief description of the shell’s operation when it reads and executes a command. Basically, the shell does the following:
# Reads its input from a file (see [[#Shell Scripts|Shell Scripts]]), from a string supplied as an argument to the -c invocation option (see [[#Invoking Bash|Invoking Bash]]), or from the user’s terminal.
#: 从文件读取它的输入(参看[[#Shell Scripts|Shell Scripts]]),从字符串参数以-c调用选项方式(参看[[#Invoking Bash|Invoking Bash]]),或者从用户终端。
# Breaks the input into words and operators, obeying the quoting rules described in [[#Quoting|Quoting]]. These tokens are separated by metacharacters. Alias expansion is performed by this step (see [[#Aliases|Aliases]]).
#: 断句,把相关的输入分解成words和操作符,它遵循[[#Quoting|Quoting]]中的引用规则。这些tokens会被元字符分开。别名扩展将按照[[#Aliases|Aliases]]步骤被执行。
# Parses the tokens into simple and compound commands (see [[#Shell Commands|Shell Commands]]).
#: 这些tokens将被解析为一些简单和复合的命令(参看[[#Shell Commands|Shell Commands]])。
# Performs the various shell expansions (see [[#Shell Expansions|Shell Expansions]]), breaking the expanded tokens into lists of filenames (see [[#Filename Expansion|Filename Expansion]]) and commands and arguments.
#: 执行不同的shell扩展(参看[[#Shell Expansions|Shell Expansions]]),断句这些扩展的tokens为文件列表(参看[[#Filename Expansion|Filename Expansion]])命令及参数。
# Performs any necessary redirections (see [[#Redirections|Redirections]]) and removes the redirection operators and their operands from the argument list.
#: 执行任何必要的重定向(参看[[#Redirections|Redirections]])并且从参数列表中移除那些重定向操作符和它们的操作数。
# Executes the command (see [[#Executing Commands|Executing Commands]]).
#: 执行命令(参看[[#Executing Commands|Executing Commands]])。
# Optionally waits for the command to complete and collects its exit status (see [[#Exit Status|Exit Status]]).
#: 可选的,等待命令的完成并收集器退出状态(参看[[#Exit Status|Exit Status]])。

2016年7月4日 (一) 09:42的最新版本



This text is a brief description of the features that are present in the Bash shell (version 4.3, 2 February 2014). The Bash home page is http://www.gnu.org/software/bash/.

这部分文字是当前Bash shell(版本4.3, 2014年2月)特性的一个简要描述。Bash的主页是http://www.gnu.org/software/bash/.

This is Edition 4.3, last updated 2 February 2014, of The GNU Bash Reference Manual, for Bash, Version 4.3.

此4.3版本更新于2014年2月,它的GNU Bash参考手册也是针对此4.3版本。

Bash contains features that appear in other popular shells, and some features that only appear in Bash. Some of the shells that Bash has borrowed concepts from are the Bourne Shell (sh), the Korn Shell (ksh), and the C-shell (csh and its successor, tcsh). The following menu breaks the features up into categories, noting which features were inspired by other shells and which are specific to Bash.

Bash所涵盖的一些特性也出现在其他流行的shells中。Bash的有些概念来自于其他的一些shells例如Bourne Shell(sh)、Korn Shell (ksh)以及C-shell(csh及其后继者,如tcsh)。以下菜单把这些特性进行了分类,并且不刻意哪些特性来自于其他的shells或者哪些特性来自于某个特定的Bash。

This manual is meant as a brief introduction to features found in Bash. The Bash manual page should be used as the definitive reference on shell behavior.


Introduction: An introduction to the shell.


Definitions: Some definitions used in the rest of this manual.


Basic Shell Features: The shell "building blocks".


Shell Builtin Commands: Commands that are a part of the shell.


Shell Variables: Variables used or set by Bash.


Bash Features: Features found only in Bash.


Job Control: What job control is and how Bash allows you to use it.


Command Line Editing: Chapter describing the command line editing features.


Using History Interactively: Command History Expansion


Installing Bash: How to build and install Bash on your system.


Reporting Bugs: How to report bugs in Bash.


Major Differences From The Bourne Shell: A terse list of the differences between Bash and historical versions of /bin/sh.


GNU Free Documentation License: Copying and sharing this documentation.


Indexes: Various indexes for this manual.



What is Bash?

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the 'Bourne-Again SHell', a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix.

Bash是shell,或者,对于GUN操作系统而言它也叫做指令语言解释器。这个名字源于"Bourne-Again SHell"的首字母缩写,Stephen Bourne的双关语,其直接始祖源自当前Unix shell sh,它始现于贝尔实验室Unix的第七个版本。

Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1). It offers functional improvements over sh for both interactive and programming use.

Bash在很大程度上与sh是兼容的,并且它也从Korn shell ksh以及C shell csh纳入了一些有用的功能。它的目的是成为IEEE POSIX Shell的一致实现,以及IEEE POSIX规范(IEEE Standard 1003.1)的工具部分。他提供了sh针对交互及编程方面的功能改善。

While the GNU operating system provides other shells, including a version of csh, Bash is the default shell. Like other GNU software, Bash is quite portable. It currently runs on nearly every version of Unix and a few other operating systems - independently-supported ports exist for MS-DOS, OS/2, and Windows platforms.

在GNU操作系统提供其他shells时,包括csh的一个版本,Bash是缺省的shell。如同其他GNU软件,Bash是非常便携的。它几乎允许于每一个版本的Unix以及其他一些操作系统 - 基于端口提供独立支持如MS-DOS,OS/2以及Windows平台。

What is a shell?

At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.


A Unix shell is both a command interpreter and a programming language. As a command interpreter, the shell provides the user interface to the rich set of GNU utilities. The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories such as /bin, allowing users or groups to establish custom environments to automate their common tasks.

Unix shell既是一个指令解释器也是一种编程语言。作为指令解释器,shell针对丰富的GUN工具提供了用户接口。而它的编程语言特性又允许这些工具可被组合起来。包含指令的文件可以被创建,而它们又成为指令的一部分。这些新指令与在其他目录中的系统指令有着相同的地位例如/bin,并允许用户或组去建立客户环境或自动化他们的公共任务。

Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.

Shells的使用可以是交互或非交互的。在交互模式下,它们接受键盘输入。在非交互执行时, shells执行可从文件读取。

A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. The redirection constructs permit fine-grained control of the input and output of those commands. Moreover, the shell allows control over the contents of commands’ environments.


Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec cannot be implemented outside of the shell because they directly manipulate the shell itself. The history, getopts, kill, or pwd builtins, among others, could be implemented in separate utilities, but they are more convenient to use as builtin commands. All of the shell builtins are described in subsequent sections.


While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.


Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases. Each of these features is described in this manual.



These definitions are used throughout the remainder of this manual.



A family of open system standards based on Unix. Bash is primarily concerned with the Shell and Utilities portion of the POSIX 1003.1 standard.
基于Unix家族的一个开放系统标准。Bash主要关心POSIX 1003.1标准的Shell以及工具部分。


A space or tab character.


A command that is implemented internally by the shell itself, rather than by an executable program somewhere in the file system.

control operator(控制操作符)

A token that performs a control function. It is a newline or one of the following: ‘||’, ‘&&’, ‘&’, ‘;’, ‘;;’, ‘|’, ‘|&’, ‘(’, or ‘)’.
一个记号并执行一个控制功能。它是一个换行符或以下东东:‘||’, ‘&&’, ‘&’, ‘;’, ‘;;’, ‘|’, ‘|&’, ‘(’, or ‘)’。

exit status(退出状态)

The value returned by a command to its caller. The value is restricted to eight bits, so the maximum value is 255.


A unit of text that is the result of one of the shell expansions. After expansion, when executing a command, the resulting fields are used as the command name and arguments.


A string of characters used to identify a file.


A set of processes comprising a pipeline, and any processes descended from it, that are all in the same process group.

job control(作业控制)

A mechanism by which users can selectively stop (suspend) and restart (resume) execution of processes.


A character that, when unquoted, separates words. A metacharacter is a blank or one of the following characters: ‘|’, ‘&’, ‘;’, ‘(’, ‘)’, ‘<’, or ‘>’.
当一个字符,当它未被引号引用,或者是单独出现。一个元字符可以是一个空格或以下单个出现的字符:‘|’,‘&’,‘;’,‘(’, ‘)’,‘<’ 或者 ‘>’。


A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.


A control operator or a redirection operator. See Redirections, for a list of redirection operators. Operators contain at least one unquoted metacharacter.
一个控制操作符或者一个重定向符。参看Redirections(重定向操作列表)。 操作符包括至少一个未引用元字符。

process group(进程组)

A collection of related processes each having the same process group ID.

process group ID(进程组ID)

A unique identifier that represents a process group during its lifetime.

reserved word(保留字)

A word that has a special meaning to the shell. Most reserved words introduce shell flow control constructs, such as for and while.

return status(返回状态)

A synonym for exit status.
退出状态(exit status)的同义词。


A mechanism by which a process may be notified by the kernel of an event occurring in the system.

special builtin(特殊内置指令)

A shell builtin command that has been classified as special by the POSIX standard.
也是shell的内置指令, 它被POSIX标准分类为特殊。


A sequence of characters considered a single unit by the shell. It is either a word or an operator.


A sequence of characters treated as a unit by the shell. Words may not include unquoted metacharacters.

Basic Shell Features

Bash is an acronym for ‘Bourne-Again SHell’. The Bourne shell is the traditional Unix shell originally written by Stephen Bourne. All of the Bourne shell builtin commands are available in Bash, The rules for evaluation and quoting are taken from the POSIX specification for the ‘standard’ Unix shell.

Bash是‘Bourne-Again SHell’的首字母缩写。Bourne shell是传统Unix shell,它诞生之初是有Stephen Bourne写就的。所有Bourne shell的内置命令在Bash中都是可用的,其评估和引用规则取自POSIX规范的Unix shell ‘standard’。

This chapter briefly summarizes the shell’s ‘building blocks’: commands, control structures, shell functions, shell parameters, shell expansions, redirections, which are a way to direct input and output from and to named files, and how the shell executes commands.

本章简述了shell的‘building blocks’(构造块):命令、控制结构、shell函数、shell 参数, shell 扩展, redirections(重定向),它是一种基于命名文件的直接输入和输出方法,以及shell怎样执行命令。

Shell Syntax: What your input means to the shell.


Shell Commands: The types of commands you can use.


Shell Functions: Grouping commands by name.


Shell Parameters: How the shell stores values.


Shell Expansions: How Bash expands parameters and the various expansions available.


Redirections: A way to control where input and output go.


Executing Commands: What happens when you run a command.


Shell Scripts: Executing files of shell commands.


Shell Syntax

Shell Operation: The basic operation of the shell.


Quoting: How to remove the special meaning from characters.


Comments: How to specify comments.


When the shell reads input, it proceeds through a sequence of operations. If the input indicates the beginning of a comment, the shell ignores the comment symbol (‘#’), and the rest of that line.


Otherwise, roughly speaking, the shell reads its input and divides the input into words and operators, employing the quoting rules to select which meanings to assign various words and characters.


The shell then parses these tokens into commands and other constructs, removes the special meaning of certain words or characters, expands others, redirects input and output as needed, executes the specified command, waits for the command’s exit status, and makes that exit status available for further inspection or processing.


Shell Operation

The following is a brief description of the shell’s operation when it reads and executes a command. Basically, the shell does the following:


  1. Reads its input from a file (see Shell Scripts), from a string supplied as an argument to the -c invocation option (see Invoking Bash), or from the user’s terminal.
    从文件读取它的输入(参看Shell Scripts),从字符串参数以-c调用选项方式(参看Invoking Bash),或者从用户终端。
  2. Breaks the input into words and operators, obeying the quoting rules described in Quoting. These tokens are separated by metacharacters. Alias expansion is performed by this step (see Aliases).
  3. Parses the tokens into simple and compound commands (see Shell Commands).
    这些tokens将被解析为一些简单和复合的命令(参看Shell Commands)。
  4. Performs the various shell expansions (see Shell Expansions), breaking the expanded tokens into lists of filenames (see Filename Expansion) and commands and arguments.
    执行不同的shell扩展(参看Shell Expansions),断句这些扩展的tokens为文件列表(参看Filename Expansion)命令及参数。
  5. Performs any necessary redirections (see Redirections) and removes the redirection operators and their operands from the argument list.
  6. Executes the command (see Executing Commands).
    执行命令(参看Executing Commands)。
  7. Optionally waits for the command to complete and collects its exit status (see Exit Status).
    可选的,等待命令的完成并收集器退出状态(参看Exit Status)。


Escape Character

Single Quotes

Double Quotes

ANSI-C Quoting

Locale-Specific Translation