bash - GNU Bourne-Again SHell
概述(SYNOPSIS)
bash [options] [file]
版权所有(COPYRIGHT)
Bash is Copyright (C) 1989-2002 by the Free Software Foundation, Inc.
描述(DESCRIPTION)
Bash 是一个与 sh 兼容的命令语言解释器,可以执行从标准输入或者文件中读取的命令。 Bash 也整合了 Korn 和 C Shell (ksh 和 csh) 中的优秀特性。
Bash 的目标是成为遵循 IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2) 的实现。
选项(OPTIONS)
除了在 set 内建命令的文档中讲述的单字符选项 (option) 之外,bash 在启动时也解释下列选项。
- -c string
- 如果有 -c 选项,那么命令将从 string 中读取。如果 string 后面有参数 (argument),它们将用于给位置参数 (positional parameter,以 $0 起始) 赋值。
- -i
- 如果有 -i 选项,shell 将交互地执行 ( interactive )。
- -l
- 选项使得 bash 以类似登录 shell (login shell) 的方式启动 (参见下面的 启动(INVOCATION) 章节)。
- -r
- 如果有 -r 选项,shell 成为受限的 ( restricted ) (参见下面的 受限的shell(RESTRICTED SHELL) 章节)。
- -s
- 如果有 -s 选项,或者如果选项处理完以后,没有参数剩余,那么命令将从标准输入读取。这个选项允许在启动一个交互的 shell 时可以设置位置参数。
- -D
- 向标准输出打印一个以 $ 为前导的,以双引号引用的字符串列表。这是在当前语言环境不是 C 或 POSIX 时,脚本中需要翻译的字符串(?)。这个选项包含了 -n 选项;不会执行命令。
- [-+]O [shopt_option]
- shopt_option 是一个 shopt 内建命令可接受的选项 (参见下面的 shell 内建命令(SHELL BUILTIN COMMANDS) 章节)。如果有 shopt_option,-O 将设置那个选项的取值; +O 取消它。如果没有给出 shopt_option,shopt 可接受的选项的名称和值将打印在标准输出上。如果启动选项是 +O,输出将显示为一种可以重新用作输入的格式。
- --
- -- 通知选项的结束,禁止其余的选项处理。任何 -- 之后的参数将作为文件名和参数对待。这相当指定了一个字符是 - 的参数(?)。
Bash 也解释一些多字节的选项。在命令行中,这些选项必须置于需要被识别的单字符参数之前。
- --dump-po-strings
- 等价于 -D,但是输出是 GNU gettext po (可移植对象) 文件格式
- --dump-strings
- 等价于 -D
- --help
- 在标准输出显示用法信息并成功退出
- --init-file file
- --rcfile file
- 如果 shell 是交互的,执行 file 中的命令,而不是标准的个人初始化文件 ~/.bashrc (参见下面的 启动(INVOCATION) 章节)
- --login
- 等价于 -l
- --noediting
- 如果 shell 是交互的,不使用 GNU readline 库来读命令行
- --noprofile
- 不读取系统范围的启动文件 /etc/profile 或者任何个人初始化文件 ~/.bash_profile, ~/.bash_login, 或 ~/.profile 。默认情况下, bash 在作为登录 shell 启动时读取这些文件 (参见下面的 启动(INVOCATION) 章节)
- --norc
- 如果 shell 是交互的,不读取/执行个人初始化文件 ~/.bashrc 这个选项在 shell 以 sh 命令启动时是默认打开的
- --posix
- 如果默认操作与 POSIX 1003.2 标准不同的话,改变 bash 的行为来符合标准 (posix mode)
- --restricted
- shell 成为受限的 (参见下面的 受限的shell(RESTRICTED SHELL) 章节)
- --rpm-requires
- 产生一个为使脚本运行,需要的文件的列表。这个选项包含了 -n 选项。它是为了避免当编译期错误检测进行时的限制-- Backticks, [] tests, 还有 evals 不会被解释,一些依赖关系可能丢失
- --verbose
- 等价于 -v
- --version
- 在标准输出显示此 bash 的版本信息并成功退出。
参数(ARGUMENTS)
如果选项处理之后仍有参数剩余,并且没有指定 -c 或 -s 选项,第一个参数将假定为一个包含 shell 命令的文件的名字。如果 bash 是以这种方式启动的, $0 将设置为这个文件的名字,位置参数将设置为剩余其他的参数。 Bash 从这个文件中读取并执行命令,然后退出。 Bash 的退出状态是脚本中执行的最后一个命令的退出状态。如果没有执行命令,退出状态是0。尝试的步骤是先试图打开在当前目录中的这个文件,接下来,如果没有找到,shell 将搜索脚本的 PATH 环境变量中的路径。
启动(INVOCATION)
login shell 登录 shell,是一个参数零的第一个字符是 - ,或者启动时指定了 --login 选项的 shell。
interactive 交互的 shell,是一个启动时没有指定非选项的参数,并且没有指定 -c 选项,标准输出和标准输入都连接到了终端 (在 isatty(3) 中判定) 的shell,或者启动时指定了 -i 选项的 shell。如果 bash 是交互的, PS1 环境变量将被设置,并且 $- 包含 i ,允许一个 shell 脚本或者一个启动文件来检测这一状态。
下列段落描述了 bash 如何执行它的启动文件。如果这些启动文件中的任一个存在但是不可读取, bash 将报告一个错误。文件名中的波浪号 (~,tilde) 将像 EXPANSION 章节中 Tilde Expansion 段描述的那样展开。
当 bash 是作为交互的登录 shell 启动的,或者是一个非交互的 shell 但是指定了 --login 选项,它首先读取并执行 /etc/profile 中的命令,只要那个文件存在。读取那个文件之后,它以如下的顺序查找 ~/.bash_profile, ~/.bash_login, 和 ~/.profile, 从存在并且可读的第一个文件中读取并执行其中的命令。 --noprofile 选项可以用来在 shell 启动时阻止它这样做。
当一个登录 shell 退出时, bash 读取并执行文件 ~/.bash_logout 中的命令,只要它存在。
当一个交互的 shell 但不是登录 shell 启动时, bash 从文件 ~/.bashrc 中读取并执行命令,只要它存在。可以用 --norc 选项来阻止它这样做。 --rcfile file 选项将强制 bash 读取并执行文件 file 中的命令,而不是 ~/.bashrc 中的。
当 bash 以非交互的方式启动时,例如在运行一个 shell 脚本时,它在环境中查找变量 BASH_ENV ,如果它存在则将它的值展开,使用展开的值作为一个文件的名称,读取并执行。 Bash 运作的过程就如同执行了下列命令:
- if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
但是没有使用 PATH 变量的值来搜索那个文件名。
如果 bash 以名称 sh 启动,它试图模仿 (mimic) sh 历史版本的启动过程,尽可能地相似,同时也遵循 POSIX 标准。
When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.
When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value. No other startup files are read.
Bash attempts to determine when it is being run by the remote shell daemon, usually rshd. If bash determines it is being run by rshd, it reads and executes commands from ~/.bashrc, if that file exists and is readable. It will not do this if invoked as sh. The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow them to be specified.
If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS variable, if it appears in the environment, is ignored, and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.
定义(DEFINITIONS)
The following definitions are used throughout the rest of this document.
- blank
- A space or tab.
- word
- A sequence of characters considered as a single unit by the shell. Also known as a token.
- name
- A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier.
- metacharacter
- A character that, when unquoted, separates words. One of the following:
-
| & ; ( ) < > space tab
- control operator
- A token that performs a control function. It is one of the following
symbols:-
|| & && ; ;; ( ) | <newline>
保留字(RESERVED WORDS)
Reserved words are words that have a special
meaning to the shell. The following words are recognized as reserved when
unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or
for command:
! case do done elif else esac fi for function if in select then until
while { } time [[ ]]
shell语法(SHELL GRAMMAR)
Simple Commands
A simple command is a sequence of optional variable assignments
followed by blank-separated words and redirections, and terminated by a
control operator. The first word specifies the command to be executed,
and is passed as argument zero. The remaining words are passed as arguments to
the invoked command.
The return value of a simple command is its exit status, or
128+n if the command is terminated by signal n.
Pipelines
A pipeline is a sequence of one or more commands separated by the
character |. The format for a pipeline is:
-
[time [-p]] [ ! ] command [ | command2 ...
]
The standard output of command is connected via a pipe to the standard
input of command2. This connection is performed before any redirections
specified by the command (see REDIRECTION below).
If the reserved word ! precedes a pipeline, the exit status of that
pipeline is the logical NOT of the exit status of the last command. Otherwise,
the status of the pipeline is the exit status of the last command. The shell
waits for all commands in the pipeline to terminate before returning a value.
If the time reserved word precedes a pipeline, the elapsed as well as
user and system time consumed by its execution are reported when the pipeline
terminates. The -p option changes the output format to that specified by
POSIX. The TIMEFORMAT variable may be set to a
format string that specifies how the timing information should be displayed; see
the description of TIMEFORMAT under Shell
Variables below.
Each command in a pipeline is executed as a separate process (i.e., in a
subshell).
Lists
A list is a sequence of one or more pipelines separated by one of the
operators ;, &, &&, or ||, and
optionally terminated by one of ;, &, or
<newline>.
Of these list operators, && and || have equal
precedence, followed by ; and &, which have equal precedence.
A sequence of one or more newlines may appear in a list instead of a
semicolon to delimit commands.
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell. The shell does not
wait for the command to finish, and the return status is 0. Commands separated
by a ; are executed sequentially; the shell waits for each command to
terminate in turn. The return status is the exit status of the last command
executed.
The control operators && and || denote AND lists and OR
lists, respectively. An AND list has the form
-
command1 && command2
command2 is executed if, and only if, command1 returns an exit
status of zero.
An OR list has the form
-
command1 || command2
command2 is executed if and only if command1 returns a non-zero
exit status. The return status of AND and OR lists is the exit status of the
last command executed in the list.
Compound Commands
A compound command is one of the following:
- (list)
- list is executed in a subshell. Variable assignments and builtin
commands that affect the shell's environment do not remain in effect after the
command completes. The return status is the exit status of list. - { list; }
- list is simply executed in the current shell environment. list
must be terminated with a newline or semicolon. This is known as a group
command. The return status is the exit status of list. Note that
unlike the metacharacters ( and ), { and } are
reserved words and must occur where a reserved word is permitted to be
recognized. Since they do not cause a word break, they must be separated from
list by whitespace. - ((expression))
- The expression is evaluated according to the rules described below
under ARITHMETICEVALUATION. If the value of
the expression is non-zero, the return status is 0; otherwise the return status
is 1. This is exactly equivalent to let "expression". - [[ expression ]]
- Return a status of 0 or 1 depending on the evaluation of the conditional
expression expression. Expressions are composed of the primaries
described below under CONDITIONALEXPRESSIONS.
Word splitting and pathname expansion are not performed on the words
between the [[ and ]]; tilde expansion, parameter and variable
expansion, arithmetic expansion, command substitution, process substitution, and
quote removal are performed.When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to the rules
described below under Pattern Matching. The return value is 0 if the
string matches or does not match the pattern, respectively, and 1 otherwise. Any
part of the pattern may be quoted to force it to be matched as a string.Expressions may be combined using the following operators, listed in
decreasing order of precedence:-
- ( expression )
- Returns the value of expression. This may be used to override the
normal precedence of operators. - ! expression
- True if expression is false.
- expression1 && expression2
- True if both expression1 and expression2 are true.
- expression1 || expression2 True if either
expression1 or expression2 is true.
The && and || operators do not evaluate
expression2 if the value of expression1 is sufficient to determine
the return value of the entire conditional expression.
- for name [ in word ] ; do list ;
done - The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in turn, and
list is executed each time. If the in word is omitted, the
for command executes list once for each positional parameter that
is set (see PARAMETERS below). The return status is
the exit status of the last command that executes. If the expansion of the items
following in results in an empty list, no commands are executed, and the
return status is 0. - for (( expr1 ; expr2 ; expr3 )) ; do
list ; done - First, the arithmetic expression expr1 is evaluated according to the
rules described below under ARITHMETICEVALUATION.
The arithmetic expression expr2 is then evaluated repeatedly until
it evaluates to zero. Each time expr2 evaluates to a non-zero value,
list is executed and the arithmetic expression expr3 is evaluated.
If any expression is omitted, it behaves as if it evaluates to 1. The return
value is the exit status of the last command in list that is executed, or
false if any of the expressions is invalid. - select name [ in word ] ; do list
; done - The list of words following in is expanded, generating a list of
items. The set of expanded words is printed on the standard error, each preceded
by a number. If the in word is omitted, the positional parameters
are printed (see PARAMETERS below). The PS3
prompt is then displayed and a line read from the standard input. If the line
consists of a number corresponding to one of the displayed words, then the value
of name is set to that word. If the line is empty, the words and prompt
are displayed again. If EOF is read, the command completes. Any other value read
causes name to be set to null. The line read is saved in the variable
REPLY. The list is executed after each selection until a
break command is executed. The exit status of select is the exit
status of the last command executed in list, or zero if no commands were
executed. - case word in [ [(] pattern [ |
pattern ] - A case command first expands word, and tries to match it
against each pattern in turn, using the same matching rules as for
pathname expansion (see Pathname Expansion below). When a match is found,
the corresponding list is executed. After the first match, no subsequent
matches are attempted. The exit status is zero if no pattern matches. Otherwise,
it is the exit status of the last command executed in list. - if list; then list; [ elif list;
then list; ] ... [ else list; ] fi - The if list is executed. If its exit status is zero, the
then list is executed. Otherwise, each elif list is
executed in turn, and if its exit status is zero, the corresponding then
list is executed and the command completes. Otherwise, the else
list is executed, if present. The exit status is the exit status of the
last command executed, or zero if no condition tested true. - while list; do list; done
- until list; do list; done
- The while command continuously executes the do list as
long as the last command in list returns an exit status of zero. The
until command is identical to the while command, except that the
test is negated; the do list is executed as long as the last
command in list returns a non-zero exit status. The exit status of the
while and until commands is the exit status of the last do
list command executed, or zero if none was executed. - [ function ] name () { list; }
- This defines a function named name. The body of the function
is the list of commands between { and }. This list is executed whenever
name is specified as the name of a simple command. The exit status of a
function is the exit status of the last command executed in the body. (See FUNCTIONS below.)
bash - GNU Bourne-Again SHell的更多相关文章
- linux bash吧,还有啥Bourne Again Shell
linux bash吧,还有啥Bourne Again Shell bash吧,还有啥Bourne Again Shell 头部要写#!/bin/bash set -x #open script de ...
- Shell: sh,bash,csh,tcsh等shell的区别(转)
转载自:http://zhidao.baidu.com/question/493376840.html, http://blog.sina.com.cn/s/blog_71261a2d0100wmbj ...
- bash shell-linux的预设shell
1.bash 是GNU 计划中重要的工具软件之一,目前也是Linux distributions 的标准shell.其主要功能如下: (1)命令记忆和历史功能,可以通过history查询,存储位置在~ ...
- 【Bash百宝箱】Linux shell学习
shell特点-- Linux有多种shell能够使用,默认的为bash,bash有以下几个主要特点. 1.命令记忆能力 在命令行中按上下键能够找到一个前/后输入的命令.这些命令记录在-/.bash_ ...
- 《学习bash》笔记--调试shell程序
在shell中,最简单的调试助手时输出语句echo,能够通过把很多echo语句放到代码中进行调试,但必须花费足够的时间以定位 要查看的信息.可能必须通过很多的输出才干发现要查找的信息. 1.set选项 ...
- 应用shell脚本停启Tomcat
最近在工作中频繁的操作多个tomcat,顺便就简单研究了一下 一. 简介 Shell 是一种与操作系统直接交互的程序,Unix系统中叫Bourne Shell,包括以下几种 Sh—Bourne She ...
- Linux shell 程序设计
shell 程序设计 主要的学习内容包含基本思路,语法:变量.条件判断和程序控制,命令列表,函数,命令及执行,调试,grep命令和正则表达式,find命令 什么是shell 适用编写执行相对简单任务的 ...
- ZZ:Solaris 10 软件包分析
ZZ:Solaris 10 软件包分析 http://blog.chinaunix.net/uid-22759617-id-276756.html # Last updated: 2006-02-14 ...
- 生产力工具:shell 与 Bash 脚本
生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...
随机推荐
- Linux文件锁【转】
本文转载自:http://blog.csdn.net/dragon_li_chen/article/details/17147911 一.文件锁的分类: 翻阅参考资料,你会发现文件锁可以进行很多的分类 ...
- YTU 2893: F--Mark的双薪
2893: F--Mark的双薪 时间限制: 1 Sec 内存限制: 128 MB 提交: 230 解决: 17 题目描述 程序员 Mark 编制的 PPYU 游戏终于上线.Mark 认为自己起早 ...
- windows 操作系统问题的解决
浏览器之间具有较大的差异: 如果某问题只存在于某浏览器,而其他浏览器不存在此问题,则说明,问题出在该浏览器上: 或者是缓存已满:⇒ 清除缓存: 或者浏览器的兼容性问题: 1. error 1327. ...
- Synchronized的使用和注意事项
synchronized: 1.取得的锁都是对象锁,而不是把一段代码或方法(函数)当作锁: 2.多个线程访问的必须是同一个对象. 3.当一个线程执行的代码出现异常时,其所持有的锁会自动释放 4.A线程 ...
- appium学习【一】:pycharm运行不生成HtmlTestRunner测试报告
参考文章和解决办法:http://blog.csdn.net/xie_0723/article/details/50825310 http://jingyan.bai ...
- hdu 2829(四边形优化 && 枚举最后一个放炸弹的地方)
Lawrence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- bzoj3620
KMP 我似乎复杂度写的不对... 因为位置相同只算一次,后缀数组什么的都不管用了,我们就暴力kmp,但是我写的是暴力跳...竟然过了...我写bzoj3670才发现... #include<c ...
- 转:Oculus Unity Development Guide开发指南(2015-7-21更新)
http://forum.exceedu.com/forum/forum.php?mod=viewthread&tid=34175 Oculus Unity Development Guide ...
- SQL 实战教程(八)
http://www.studyofnet.com/news/247.html 1.修改字段为自增 alter table [dbo].[Logs] drop column ID alter tabl ...
- CMake学习笔记三:cmake 常用指令
1 基本指令 1,ADD_DEFINITIONS 向 C/C++编译器添加-D 定义,比如: DD_DEFINITIONS(-DENABLE_DEBUG -DABC),参数之间用空格分割. 如果你的代 ...