<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
	<id>https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Bash</id>
	<title>Bash - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=Bash"/>
	<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Bash&amp;action=history"/>
	<updated>2026-07-14T00:07:14Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://arolstar52-zhtest.hf.space/index.php?title=Bash&amp;diff=160067&amp;oldid=prev</id>
		<title>imported&gt;Mhss：​/* top */</title>
		<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=Bash&amp;diff=160067&amp;oldid=prev"/>
		<updated>2026-03-24T00:32:39Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;top&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{noteTA&lt;br /&gt;
|G1=IT&lt;br /&gt;
|G2=FL&lt;br /&gt;
}}&lt;br /&gt;
{{Infobox software&lt;br /&gt;
| name = Bash&lt;br /&gt;
| logo = Gnu-bash-logo.svg&lt;br /&gt;
| logo_size =  240px&lt;br /&gt;
| screenshot =&lt;br /&gt;
| caption = Bash會話的截圖&lt;br /&gt;
| author = [[布萊恩·福克斯]]&lt;br /&gt;
| developer = Chet Ramey&lt;br /&gt;
| released = {{start date and age|1989|06|08}}&lt;br /&gt;
| programming language = [[C語言]]&lt;br /&gt;
| operating system = {{Plainlist|&lt;br /&gt;
* [[Unix-like]],&amp;lt;ref&amp;gt;{{cite web|url=ftp://ftp.cwru.edu/pub/bash/FAQ|archive-url=https://web.archive.org/web/20180901171316/ftp://ftp.cwru.edu/pub/bash/FAQ|archive-date=2018-09-01|dead-url=yes|title=Bash FAQ, version 4.14|access-date=2016-04-09}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[macOS]]（系統包括較舊的[[GNU通用公共许可证#GPLv2|GPLv2]]版本；當前版本可通過第三方獲得）&amp;lt;ref&amp;gt;{{cite web|url=http://apple.stackexchange.com/questions/208312/why-does-apple-ship-bash-3-2|title=Why does Apple ship bash 3.2?|website=apple.stackexchange.com|accessdate=2019-04-25|archive-date=2016-04-16|archive-url=https://web.archive.org/web/20160416165331/http://apple.stackexchange.com/questions/208312/why-does-apple-ship-bash-3-2|dead-url=no}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[Windows]]（透過[[适用于Linux的Windows子系统|WSL]]提供，為較新的[[GNU通用公共许可证#GPLv3|GPLv3+]]版本）&amp;lt;ref&amp;gt;{{cite web|url=https://github.com/Microsoft/BashOnWindows/issues/107|title=Missing source code - GPL compliance? · Issue #107 · Microsoft/WSL|website=GitHub|accessdate=2019-04-25|archive-date=2019-09-24|archive-url=https://web.archive.org/web/20190924163108/https://github.com/microsoft/WSL/issues/107|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|title=GNU Bash|url=http://www.softpedia.com/get/System/System-Miscellaneous/GNU-Bash.shtml|website=[[Softpedia]]|publisher=SoftNews|accessdate=2016-04-09|archive-date=2017-10-21|archive-url=https://web.archive.org/web/20171021004857/http://www.softpedia.com/get/System/System-Miscellaneous/GNU-Bash.shtml|dead-url=no}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
 }}&lt;br /&gt;
| platform = [[IA-32]]，[[x86-64]]，[[ppc64|ppc64el]]，[[ARM64]]，[[RISC-V]] 等&lt;br /&gt;
| language = 多語言（[[gettext]]）&lt;br /&gt;
| genre = [[Unix shell]]、[[命令語言]]&lt;br /&gt;
| license = [[GNU GPL#Version 3|GPLv3+]]&amp;lt;ref name=&amp;quot;license&amp;quot;&amp;gt;{{cite web | author = GNU Project | authorlink = GNU Project | title = README file | url = https://www.gnu.org/software/bash/ | quote = Bash is free software, distributed under the terms of the [GNU] General Public License as published by the Free Software Foundation, version 3 of the License (or any later version). | accessdate = 2019-04-25 | archive-date = 2019-04-26 | archive-url = https://web.archive.org/web/20190426210620/http://www.gnu.org/software/bash/ | dead-url = no }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| website = {{URL|https://www.gnu.org/software/bash/}}&lt;br /&gt;
}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Bash&amp;#039;&amp;#039;&amp;#039;，[[Unix shell]]的一種，在1987年由[[布萊恩·福克斯]]為了[[GNU計劃]]而编写。1989年釋出第一個正式版本，原先是計劃用在[[GNU]]作業系統上，但能运行于大多数[[类Unix系统]]的操作系统之上，包括[[Linux]]與[[Mac OS X v10.4]]起至[[macOS Mojave]]都將它作為預設shell，而自[[macOS Catalina]]，預設Shell以[[zsh]]取代。&lt;br /&gt;
&lt;br /&gt;
Bash是[[Bourne shell]]的後繼相容版本與開放原始碼版本，它的名稱來自[[Bourne shell]]（sh）的一个双关语（&amp;#039;&amp;#039;Bourne again&amp;#039;&amp;#039; / born again）：&amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;ourne-&amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039;gain &amp;#039;&amp;#039;&amp;#039;SH&amp;#039;&amp;#039;&amp;#039;ell。&lt;br /&gt;
&lt;br /&gt;
Bash是一个命令处理器，通常运行于文本窗口中，并能执行用户直接输入的命令。Bash还能从文件中读取命令，这样的文件称为脚本。和其他Unix shell 一样，它支持文件名替换（通配符匹配）、[[管道 (Unix)|管道]]、[[here文档]]、命令替换、变量，以及条件判断和循环遍历的结构控制语句。包括关键字、语法在内的基本特性全部是从[[Bourne shell|sh]]借鉴过来的。其他特性，例如历史命令，是从[[C Shell|csh]]和[[Korn shell|ksh]]借鉴而来。总的来说，Bash虽然是一个满足[[POSIX]]规范的shell，但有很多扩展。&lt;br /&gt;
&lt;br /&gt;
一个名为[[Shellshock]]的安全漏洞在2014年9月初被发现，并迅速导致互联网上的一系列攻击。这个漏洞可追溯到1989年发布的1.03版本。&lt;br /&gt;
&lt;br /&gt;
== 历史 ==&lt;br /&gt;
由于[[理查德·斯托曼]]对于之前一位开发者的进度不满，布莱恩·福克斯从1988年1月10日开始开发Bash。斯托曼和[[自由软件基金会]]希望到一个能够运行已有的shell脚本的自由软件。他们把这看作是建成一个基于BSD和GNU的完全自由的操作系统的战略的重要部分。这是他们自己注资的几个项目之一。福克斯作为自由软件基金会的雇员承担这项工作。1989年6月8日，福克斯发布Bash的beta版本，版本号为.99。在福克斯离开于1992年中期到1994年中期的某个时候离开自由软件基金会之前，他一直担任Bash的主要维护者。之后，他的工作被传递给另一个早期贡献者，切特·雷米（Chet Ramey）。&lt;br /&gt;
&lt;br /&gt;
从那时起，在Linux用户当中sh在很大度上成为最流行的shell，并成为许多Linux发行版默认的交互式shell（不过[[Almquist shell]]可能是默认的脚本shell）。在苹果公司的 OS X 操作系统上也是如此。Bash 也被移植到 Microsoft Windows（通过[[Cygwin]]和[[MinGW]]）。通过[[DJGPP]]项目，Bash被移植到了[[DOS]]。通过许多终端模拟软件，Bash被移植到[[Novell NetWare]]和[[Android]]。微软在2016年的Build大会上宣布，Windows 10 添加一个Linux子系统，完全支持Bash和其他Ubuntu下的二进制程序。&lt;br /&gt;
&lt;br /&gt;
2014年9月24日，Stephane Chazelas，一位工作于英国，致力于Unix/Linux和网络通信方面的专家，发现Bash的一个安全漏洞。这个漏洞被命名为Shellshock，并被分配编号 CVE-2014-6271、CVE-2014-6277、CVE-2014-7169。这个漏洞非常严重，因为使用Bash的[[通用网关接口|CGI]]脚本会变得脆弱，使得攻击者可以执行任意的代码。这个漏洞与Bash通过环境变量把函数定义传递给shell子进程的方式有关。&lt;br /&gt;
&lt;br /&gt;
== 语法与特性 ==&lt;br /&gt;
bash的命令语法是Bourne shell命令语法的超集。数量庞大的Bourne shell脚本大多不经修改即可以在bash中执行，只有那些引用了Bourne特殊变量或使用了Bourne的内置命令的脚本才需要修改。bash的命令语法很多来自[[Korn shell]]（ksh）和[[C Shell]]（csh），例如命令行编辑，命令历史，目录栈，&amp;lt;tt&amp;gt;$RANDOM&amp;lt;/tt&amp;gt;和&amp;lt;tt&amp;gt;$PPID&amp;lt;/tt&amp;gt;变量，以及[[POSIX]]的命令置换语法：&amp;lt;tt&amp;gt;$(...)&amp;lt;/tt&amp;gt;。作为一个交互式的shell，按下TAB键即可自动补全已部分输入的程序名，文件名，变量名等等。&lt;br /&gt;
&lt;br /&gt;
使用&amp;#039;function&amp;#039;关键字时，Bash的函数声明与Bourne/Korn/POSIX脚本不兼容（Korn shell 有同样的问题）。不过Bash也接受Bourne/Korn/POSIX的函数声明语法。因为许多不同，Bash脚本很少能在Bourne或Korn解释器中运行，除非编写脚本时刻意保持兼容性。然而，随着Linux的普及，这种方式正变得越来越少。不过在POSIX模式下，Bash更加符合POSIX。&lt;br /&gt;
&lt;br /&gt;
bash的语法针对Bourne shell的不足做了很多扩展。其中的一些列举在这里。&lt;br /&gt;
&lt;br /&gt;
=== 花括号扩展 ===&lt;br /&gt;
花括号扩展是一个从C shell借鉴而来的特性，它产生一系列指定的字符串（按照原先从左到右的顺序）。这些字符串不需要是已经存在的文件。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ echo a{p,c,d,b}e&lt;br /&gt;
ape ace ade abe&lt;br /&gt;
$ echo {a,b,c}{d,e,f}&lt;br /&gt;
ad ae af bd be bf cd ce cf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;花括号扩展不应该被用在可移植的shell脚本中，因为Bourne shell产生的结果不同。&amp;lt;syntaxhighlight lang=&amp;quot;shell&amp;quot;&amp;gt;&lt;br /&gt;
#! /bin/sh&lt;br /&gt;
&lt;br /&gt;
# 传统的shell并不产生相同结果&lt;br /&gt;
echo a{p,c,d,b}e # a{p,c,d,b}e&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;当花括号扩展和通配符一起使用时，花括号扩展首先被解析，然后正常解析通配符。因此，可以用这种方法获得当前目录的一系列JPEG和PNG文件。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls *.{jpg,jpeg,png}    # 首先扩展为*.jpg *.jpeg *.png，然后解析通配符&lt;br /&gt;
echo *.{png,jp{e,}g}   # echo显示扩展结果；花括号扩展可以嵌套。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;除了列举备选项，还可以用“..”在花括号扩展中指定字符或数字范围。较新的Bash版本接受一个整数作为第三个参数，指定增量。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ echo {1..10}&lt;br /&gt;
1 2 3 4 5 6 7 8 9 10&lt;br /&gt;
$ echo file{1..4}.txt&lt;br /&gt;
file1.txt file2.txt file3.txt file4.txt&lt;br /&gt;
$ echo {a..e}&lt;br /&gt;
a b c d e&lt;br /&gt;
$ echo {1..10..3}&lt;br /&gt;
1 4 7 10&lt;br /&gt;
$ echo {a..j..3}&lt;br /&gt;
a d g j&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;当花括号扩展和变量扩展一起使用时，变量扩展解析于花括号扩展之后。有时有必要使用内置的&amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;函数&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ start=1; end=10&lt;br /&gt;
$ echo {$start..$end}  # 由于解析顺序，无法得到想要的结果&lt;br /&gt;
{1..10}&lt;br /&gt;
$ eval echo {$start..$end} # 首先进行变量扩展的解析&lt;br /&gt;
1 2 3 4 5 6 7 8 9 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 使用整数 ===&lt;br /&gt;
&lt;br /&gt;
与Bourne shell不同的是bash不用另外生成进程即能进行整数运算。bash使用&amp;lt;tt&amp;gt;((...))&amp;lt;/tt&amp;gt;命令和&amp;lt;tt&amp;gt;$[...]&amp;lt;/tt&amp;gt;变量语法来达到这个目的：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 VAR=55             # 将整数55赋值给变量VAR&lt;br /&gt;
 ((VAR = VAR + 1))  # 变量VAR加1。注意这里没有&amp;#039;$&amp;#039;&lt;br /&gt;
 ((++VAR))          # 另一种方法给VAR加1。使用C语言风格的前缀自增&lt;br /&gt;
 ((VAR++))          # 另一种方法给VAR加1。使用C语言风格的后缀自增&lt;br /&gt;
 echo $((VAR * 22)) # VAR乘以22并将结果送入命令&lt;br /&gt;
 echo $[VAR * 22]   # 同上，但为过时用法&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;((...))&amp;lt;/tt&amp;gt;命令可以用于条件语句，因为它的退出状态是0或者非0（大多数情况下是1），可以用于是与非的条件判断：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 if((VAR == Y * 3 + X * 2))&lt;br /&gt;
 then&lt;br /&gt;
         echo Yes&lt;br /&gt;
 fi&lt;br /&gt;
&lt;br /&gt;
 ((Z &amp;gt; 23)) &amp;amp;&amp;amp; echo Yes&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;((...))&amp;lt;/tt&amp;gt;命令支持下列比较操作符：&amp;#039;&amp;lt;tt&amp;gt;==&amp;lt;/tt&amp;gt;&amp;#039;, &amp;#039;&amp;lt;tt&amp;gt;!=&amp;lt;/tt&amp;gt;&amp;#039;, &amp;#039;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&amp;#039;, &amp;#039;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&amp;#039;, &amp;#039;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;&amp;gt;=&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&amp;#039;，和&amp;#039;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;=&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&amp;#039;。&lt;br /&gt;
&lt;br /&gt;
bash不能在自身进程内进行[[浮点数]]运算。当前有这个能力的unix shell只有[[KornShell]]和[[Z shell]]。&lt;br /&gt;
&lt;br /&gt;
=== 输入输出重定向 ===&lt;br /&gt;
bash拥有传统Bourne shell缺乏的I/O重定向语法。bash可以同时重定向标准输出和标准错误，这需要使用下面的语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 command &amp;amp;&amp;gt; file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
这比等价的Bourne shell语法&amp;quot;&amp;lt;tt&amp;gt;command &amp;gt; file 2&amp;gt;&amp;amp;1&amp;lt;/tt&amp;gt;&amp;quot;来的简单。2.05b版本以后，bash可以用下列语法重定向标准输入至字符串（称为here string）：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 command &amp;lt;&amp;lt;&amp;lt; &amp;quot;string to be read as standard input&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
如果字符串包括空格就需要用引号包裹字符串。&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;例子&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
重定向标准输出至文件，写数据，关闭文件，重置标准输出。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 # 生成标准输出（文件描述符1）的拷贝文件描述符6&lt;br /&gt;
 exec 6&amp;gt;&amp;amp;1&lt;br /&gt;
 # 打开文件&amp;quot;test.data&amp;quot;以供写入&lt;br /&gt;
 exec 1&amp;gt;test.data&lt;br /&gt;
 # 产生一些内容&lt;br /&gt;
 echo &amp;quot;data:data:data&amp;quot;&lt;br /&gt;
 # 关闭文件&amp;quot;test.data&amp;quot;&lt;br /&gt;
 exec 1&amp;gt;&amp;amp;-&lt;br /&gt;
 # 使标准输出指向FD 6（重置标准输出）&lt;br /&gt;
 exec 1&amp;gt;&amp;amp;6&lt;br /&gt;
 # 关闭FD6&lt;br /&gt;
 exec 6&amp;gt;&amp;amp;-&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
打开及关闭文件&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 # 打开文件test.data以供读取&lt;br /&gt;
 exec 6&amp;lt;test.data&lt;br /&gt;
 # 读文件直到文件尾&lt;br /&gt;
 while read -u 6 dta&lt;br /&gt;
 do&lt;br /&gt;
   echo &amp;quot;$dta&amp;quot;&lt;br /&gt;
 done&lt;br /&gt;
 # 关闭文件test.data&lt;br /&gt;
 exec 6&amp;lt;&amp;amp;-&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
抓取外部命令的输出&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  # 运行&amp;#039;find&amp;#039;并且将结果存于VAR&lt;br /&gt;
  # 搜索以&amp;quot;h&amp;quot;结尾的文件名&lt;br /&gt;
  VAR=$(find . -name &amp;quot;*h&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 进程内的正则表达式 ===&lt;br /&gt;
&lt;br /&gt;
bash 3.0支持进程内的[[正则表达式]]，使用下面的语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 [[ string =~ regex ]]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
正则表达式语法同regex(7) man page所描述的一致。正则表达式匹配字符串时上述命令的退出状态为0，不匹配为1。正则表达式中用圆括号括起的子表达式可以访问shell变量&amp;lt;tt&amp;gt;BASH_REMATCH&amp;lt;/tt&amp;gt;，如下：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 if [[ abcfoobarbletch =~ &amp;#039;foo(bar)bl(.*)&amp;#039; ]]&lt;br /&gt;
 then&lt;br /&gt;
         echo The regex matches!&lt;br /&gt;
         echo $BASH_REMATCH      -- outputs: foobarbletch&lt;br /&gt;
         echo ${BASH_REMATCH[1]} -- outputs: bar&lt;br /&gt;
         echo ${BASH_REMATCH[2]} -- outputs: etch&lt;br /&gt;
 fi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
使用这个语法的性能要比生成一个新的进程来运行&amp;lt;tt&amp;gt;grep&amp;lt;/tt&amp;gt;命令优越，因为正则表达式匹配在bash进程内完成。如果正则表达式或者字符串包括空格或者shell [[关键字]]，（诸如&amp;#039;&amp;lt;tt&amp;gt;*&amp;lt;/tt&amp;gt;&amp;#039;或者&amp;#039;&amp;lt;tt&amp;gt;?&amp;lt;/tt&amp;gt;&amp;#039;），就需要用引号包裹。Bash 4 开始的版本已经不需要这么做了。&lt;br /&gt;
&lt;br /&gt;
=== 转义字符 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;$&amp;#039;string&amp;#039;&amp;lt;/tt&amp;gt; 形式的字符串会被特殊处理。字符串会被展开成&amp;lt;tt&amp;gt;string&amp;lt;/tt&amp;gt;，并像[[C语言]]那样将反斜杠及紧跟的字符进行替换。反斜杠转义序列的转换方式如下：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ 转义字符&lt;br /&gt;
|-&lt;br /&gt;
! 转义字符 !! 扩展成...&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\a&amp;lt;/tt&amp;gt; || 响铃符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\b&amp;lt;/tt&amp;gt; || 退格符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\e&amp;lt;/tt&amp;gt; || ANSI转义符，等价于&amp;lt;tt&amp;gt;\033&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\f&amp;lt;/tt&amp;gt; || 馈页符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; || 换行符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\r&amp;lt;/tt&amp;gt; || 回车符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\t&amp;lt;/tt&amp;gt; || 水平制表符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\v&amp;lt;/tt&amp;gt; || 垂直制表符&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\\&amp;lt;/tt&amp;gt; || 反斜杠&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\&amp;#039;&amp;lt;/tt&amp;gt; || 单引号&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\nnn&amp;lt;/tt&amp;gt; || 十进制值为nnn的8-bit字符（1－3位）&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\xHH&amp;lt;/tt&amp;gt; || 十六进制值为HH的8-bit字符（1或2位）&lt;br /&gt;
|-&lt;br /&gt;
| align=&amp;quot;center&amp;quot; | &amp;lt;tt&amp;gt;\cx&amp;lt;/tt&amp;gt; || control-X字符&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
扩展后的结果将被单引号包裹，就好像美元符号一直就不存在一样。&lt;br /&gt;
&lt;br /&gt;
双引号包裹的字符串前若有一个美元符号（&amp;lt;tt&amp;gt;$&amp;quot;...&amp;quot;&amp;lt;/tt&amp;gt;）将会使得字符串被翻译成符合当前locale的语言。如果当前locale是C或者POSIX，美元符号会被忽略。如果字符串被翻译并替换，替换后的字符串仍被双引号包裹。&lt;br /&gt;
&lt;br /&gt;
=== 关联数组 ===&lt;br /&gt;
Bash 4.0 开始支持关联数组，通过类似[[Awk|AWK]]的方式，对于多维数组提供了伪支持。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ declare -A a         # 声明一个名为a的伪二位数组&lt;br /&gt;
$ i=1; j=2&lt;br /&gt;
$ a[$i,$j]=5           # 将键 &amp;quot;$i,$j&amp;quot; 与值 5 对应&lt;br /&gt;
$ echo ${a[$i,$j]}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 移植性 ===&lt;br /&gt;
调用Bash时指定 &amp;lt;code&amp;gt;--posix&amp;lt;/code&amp;gt; 或者在脚本中声明 &amp;lt;code&amp;gt;set -o posix&amp;lt;/code&amp;gt; ，可以使得Bash几乎遵循 POSIX 1003.2 标准。若要保证一个Bash脚本的移植性，至少需要考虑到 Bourne shell，即Bash取代的shell。Bash有一些传统的 Bourne shell 所没有的特性，包括以下这些：&lt;br /&gt;
* 某些扩展的调用选项&lt;br /&gt;
* 命令替换（即&amp;lt;code&amp;gt;$()&amp;lt;/code&amp;gt;）（尽管这是 POSIX 1003.2 标准的一部分）&lt;br /&gt;
* 花括号扩展&lt;br /&gt;
* 某些数组操作、关联数组&lt;br /&gt;
* 扩展的双层方括号判断语句&lt;br /&gt;
* 某些字符串生成操作&lt;br /&gt;
* 进程替换&lt;br /&gt;
* 正则表达式匹配符&lt;br /&gt;
* Bash特有的内置工具&lt;br /&gt;
* 协进程&lt;br /&gt;
&lt;br /&gt;
=== 键盘快捷键 ===&lt;br /&gt;
Bash默认使用Emacs的快捷键，可以通过 &amp;lt;code&amp;gt;set -o vi&amp;lt;/code&amp;gt; 让它使用Vi的快捷键&lt;br /&gt;
&lt;br /&gt;
=== 进程管理 ===&lt;br /&gt;
Bash有两种执行命令的模式：批处理模式、并发模式。&lt;br /&gt;
&lt;br /&gt;
==== 批处理模式 ====&lt;br /&gt;
要以 批处理模式 执行命令（即按照顺序），必须用&amp;lt;code&amp;gt;;&amp;lt;/code&amp;gt;分隔，例子如下：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command1 ; command2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
在这个例子中，当&amp;lt;code&amp;gt;command1&amp;lt;/code&amp;gt;执行完毕，即执行&amp;lt;code&amp;gt;command2&amp;lt;/code&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
==== 并发模式 ====&lt;br /&gt;
使&amp;lt;code&amp;gt;command1&amp;lt;/code&amp;gt;后台运行，则单独在结尾处使用&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;，例子如下：&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command1 &amp;amp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
要并发执行两个命令，它们必须用&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;分隔，例子如下：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command1 &amp;amp; command2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
在这种情况下，&amp;lt;code&amp;gt;command1&amp;lt;/code&amp;gt; 在后台执行（通过&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;），从而立即将控制返回到shell，以执行&amp;lt;code&amp;gt;command2&amp;lt;/code&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
通过 {{键盘按钮|Control|Z}} 可以将当前进程[[挂起]]（放置后台并暂停运行），可通过 &amp;lt;code&amp;gt;fg&amp;lt;/code&amp;gt; 命令恢复至前台，也通过&amp;lt;code&amp;gt;bg&amp;lt;/code&amp;gt;将挂起的进程后台运行。&lt;br /&gt;
&lt;br /&gt;
==== 查看进程状态 ====&lt;br /&gt;
所有进程的情况，我们可以通过&amp;lt;code&amp;gt;jobs&amp;lt;/code&amp;gt;命令查看，包含正在后台运行和停止了的。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ jobs&lt;br /&gt;
[1]-  Running                  command1 &amp;amp;&lt;br /&gt;
[2]+  Stopped                  command2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;上例的输出结果中，中括号包围的数字（如： &amp;#039;&amp;#039;&amp;#039;&amp;quot;&amp;#039;&amp;#039;&amp;#039;[1]&amp;quot; 和 &amp;quot;[2]&amp;quot; ）为job的ID号。加号(+)用于指定&amp;lt;code&amp;gt;fg&amp;lt;/code&amp;gt;和&amp;lt;code&amp;gt;bg&amp;lt;/code&amp;gt;命令的默认对象job。减号( - )用于指定，若当前的默认对象job退出后，下一个默认对象job是谁。&amp;quot;Running&amp;quot; 和 &amp;quot;Stopped&amp;quot;  表示进程状态。 最后一个字段为命令。&amp;lt;ref&amp;gt;{{Cite web|title=jobs.1p - Linux manual page|url=http://man7.org/linux/man-pages/man1/jobs.1p.html|accessdate=2020-01-14|work=man7.org|archive-date=2020-01-14|archive-url=https://web.archive.org/web/20200114050250/http://man7.org/linux/man-pages/man1/jobs.1p.html|dead-url=no}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
总结：&lt;br /&gt;
&lt;br /&gt;
* 一般命令在前台执行（&amp;lt;code&amp;gt;fg&amp;lt;/code&amp;gt;），执行完毕后，控制返回给用户。&lt;br /&gt;
* 在命令后面加上&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;，它会在后台执行（&amp;lt;code&amp;gt;bg&amp;lt;/code&amp;gt;），并将特殊的环境变量&amp;lt;code&amp;gt;$!&amp;lt;/code&amp;gt;设置为该任务的进程ID。这时shell可以并发执行其他命令。&lt;br /&gt;
* 后台程序试图写入数据到终端设备时（与写入标准输出不同）可能被阻塞。&lt;br /&gt;
* shell可以等待一个后台任务执行完成，只需使用&amp;lt;code&amp;gt;wait&amp;lt;/code&amp;gt;命令，加上进程ID或者任务序号；也可以等待所有的后台任务，只需使用不加参数的&amp;lt;code&amp;gt;wait&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 启动脚本 ==&lt;br /&gt;
bash启动的时候会运行各种不同的脚本。&lt;br /&gt;
&lt;br /&gt;
当bash作为一个登录的交互shell被调用，或者作为非交互shell但带有&amp;lt;tt&amp;gt;--login&amp;lt;/tt&amp;gt;参数被调用时，它首先读入并执行文件&amp;lt;tt&amp;gt;/etc/profile&amp;lt;/tt&amp;gt;。然后它会依次寻找&amp;lt;tt&amp;gt;~/.bash_profile&amp;lt;/tt&amp;gt;，&amp;lt;tt&amp;gt;~/.bash_login&amp;lt;/tt&amp;gt;，和&amp;lt;tt&amp;gt;~/.profile&amp;lt;/tt&amp;gt;，读入并执行第一个存在且可读的文件。&amp;lt;tt&amp;gt;--noprofile&amp;lt;/tt&amp;gt;参数可以阻止bash启动时的这种行为。&lt;br /&gt;
&lt;br /&gt;
当一个登录shell退出时，bash读取并执行&amp;lt;tt&amp;gt;~/.bash_logout&amp;lt;/tt&amp;gt;文件，如果此文件存在。&lt;br /&gt;
&lt;br /&gt;
当一个交互的非登录shell启动后，bash读取并执行&amp;lt;tt&amp;gt;~/.bashrc&amp;lt;/tt&amp;gt;文件。这个行为可以用&amp;lt;tt&amp;gt;--norc&amp;lt;/tt&amp;gt;参数阻止。&amp;lt;tt&amp;gt;--rcfile file&amp;lt;/tt&amp;gt;参数强制bash读取并执行指定的&amp;lt;tt&amp;gt;file&amp;lt;/tt&amp;gt;而不是默认的&amp;lt;tt&amp;gt;~/.bashrc&amp;lt;/tt&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
如果用&amp;lt;tt&amp;gt;sh&amp;lt;/tt&amp;gt;来调用bash，bash在启动后进入&amp;#039;&amp;#039;posix&amp;#039;&amp;#039;模式，它会尽可能模仿sh历史版本的启动行为，以便遵守[[POSIX]]标准。用&amp;lt;tt&amp;gt;sh&amp;lt;/tt&amp;gt;名字调用的非交互shell不会去读取其他启动脚本，&amp;lt;tt&amp;gt;--rcfile&amp;lt;/tt&amp;gt;参数无效。&lt;br /&gt;
&lt;br /&gt;
当bash以POSIX模式启动时（例如带有&amp;lt;tt&amp;gt;--posix&amp;lt;/tt&amp;gt;参数）它使用POSIX标准来读取启动文件。在此模式下，交互shells扩展变量&amp;lt;tt&amp;gt;ENV&amp;lt;/tt&amp;gt;，从以此为文件名的文件中读取命令并执行。&lt;br /&gt;
&lt;br /&gt;
bash会探测自己是不是被远程shell守护程序运行（通常是&amp;lt;tt&amp;gt;rshd&amp;lt;/tt&amp;gt;）。如果是，它会读取并执行&amp;lt;tt&amp;gt;~/.bashrc&amp;lt;/tt&amp;gt;中的命令。但是&amp;lt;tt&amp;gt;rshd&amp;lt;/tt&amp;gt;一般不会用rc相关参数调用shell，也不会允许指定这些参数。&lt;br /&gt;
&lt;br /&gt;
=== Bash与Bourne shell和csh启动脚本的比较 ===&lt;br /&gt;
Bash的特性是从Bourne shell和csh发展而来，因此一定程度上允许同Bourne shell的启动文件共享，并提供一些csh用户熟悉的启动特性。&lt;br /&gt;
&lt;br /&gt;
==== 设置可继承的环境变量 ====&lt;br /&gt;
Bourne shell登陆时使用 &amp;lt;code&amp;gt;~/.profile&amp;lt;/code&amp;gt; 来设置环境变量，这些环境变量可以被子进程继承。Bash可以以兼容的方式使用&amp;lt;code&amp;gt;~/.profile&amp;lt;/code&amp;gt; ，只需在Bash自有的脚本中显式执行下面这行代码。通过在&amp;lt;code&amp;gt;~/.profile&amp;lt;/code&amp;gt; 中避免使用Bash特有的语法，就可以和Bourne shell保持兼容性。&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
. ~/.profile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 别名和函数 ====&lt;br /&gt;
更通用的函数以及借鉴自csh的“别名(alias)”很大程度上取代了Bourne shell的别名(alias)和函数。然而这两个特性一般不能从登录式shell中继承，在该登录式shell的子shell中，它们必须被重新定义。尽管有个环境变量&amp;lt;code&amp;gt;ENV&amp;lt;/code&amp;gt;可以被用于这个问题，不过 csh 和 Bash 都可以用子shell的启动脚本直接处理。在Bash当中，&amp;lt;code&amp;gt;~/.bashrc&amp;lt;/code&amp;gt; 是交互式子shell启动时执行的脚本。如果想要在登录式shell中使用 &amp;lt;code&amp;gt;~/.bashrc&amp;lt;/code&amp;gt; 定义的函数，可以在 &amp;lt;code&amp;gt;~/.bash_login&amp;lt;/code&amp;gt; 的环境变量后面加上这样一行：&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
. ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 登录与登出时执行的命令 ====&lt;br /&gt;
最初登录时，csh 执行 &amp;lt;code&amp;gt;~/.login&amp;lt;/code&amp;gt; ，可以执行一些只在登录时执行的操作，例如显示系统负载、硬盘状态、是否收到新邮件、在日志中记录登录时间，等等。Bourne shell 可以在 &amp;lt;code&amp;gt;~/.profile&amp;lt;/code&amp;gt; 文件中模拟这种行为，但并没有预先定义文件名。可以在 &amp;lt;code&amp;gt;~/.bash_profile&amp;lt;/code&amp;gt; 文件的环境变量设置和函数定义的后面添加这样一行：&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
. ~/.bash_login&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;与之类似，csh还有一个文件 &amp;lt;code&amp;gt;~/.logout&amp;lt;/code&amp;gt; ，这个文件只在登录式shell退出时执行。Bash与之对应的文件是 &amp;lt;code&amp;gt;~/.bash_logout&amp;lt;/code&amp;gt; ，并且不需要专门的设置。在 Bourne shell 中，&amp;lt;code&amp;gt;trap&amp;lt;/code&amp;gt; 这个内置工具可以实现类似的效果。&lt;br /&gt;
&lt;br /&gt;
====兼容旧环境的Bash启动脚本示例====&lt;br /&gt;
下面这个 &amp;lt;code&amp;gt;~/.bash_profile&amp;lt;/code&amp;gt; 的框架与 Bourne shell 兼容，并且为 &amp;lt;code&amp;gt;~/.bashrc&amp;lt;/code&amp;gt; 和 &amp;lt;code&amp;gt;~/.bash_login&amp;lt;/code&amp;gt; 提供类似于 csh 的语义。&amp;lt;code&amp;gt;[ -r &amp;#039;&amp;#039;文件名&amp;#039;&amp;#039;]&amp;lt;/code&amp;gt; 测试指定文件是否存在，如果不存在，跳过 &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt; 后面的部分&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[ -r ~/.profile ] &amp;amp;&amp;amp; . ~/.profile             # 只使用Bourne shell的语法设置环境变量&lt;br /&gt;
if [ -n &amp;quot;$PS1&amp;quot; ] ; then                       # 判断是否是交互式shell&lt;br /&gt;
   [ -r ~/.bashrc     ] &amp;amp;&amp;amp; . ~/.bashrc        # 加载~/.bashrc（tty、prompt、函数设置等）&lt;br /&gt;
   [ -r ~/.bash_login ] &amp;amp;&amp;amp; . ~/.bash_login    # 执行登录式shell登录时的任务&lt;br /&gt;
fi                                            # if块的结束标志&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Bash 启动脚本与操作系统相关的问题===&lt;br /&gt;
一些 Unix 和 Linux 版本常在 &amp;lt;code&amp;gt;/etc&amp;lt;/code&amp;gt; 放置 Bash 的系统级启动脚本。Bash在其标准的初始化过程中执行它们，不过其他启动脚本可以按照不同于Bash启动序列文档所述的顺序来读取这些文件。root 用户的文件默认内容，以及新用户被创建时系统提供的默认文件可能有问题。启动 [[X Window系統|X Window系统]] 的启动脚本可能使用用户的 Bash 启动脚本尝试在 [[窗口管理器]] 启动之前设置用户的环境变量。这些问题常常可以通过使用 &amp;lt;code&amp;gt;~/.xsession&amp;lt;/code&amp;gt; 或者 &amp;lt;code&amp;gt;~/.xprofile&amp;lt;/code&amp;gt; 来读取 &amp;lt;code&amp;gt;~/.profile&amp;lt;/code&amp;gt; 而解决。&lt;br /&gt;
&lt;br /&gt;
== 参见 ==&lt;br /&gt;
{{Portal|自由軟體}}&lt;br /&gt;
* [[Unix shell]]&lt;br /&gt;
&lt;br /&gt;
==脚注==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== 外部链接 ==&lt;br /&gt;
*[https://web.archive.org/web/20060813115916/http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html GNU Bash主页]&lt;br /&gt;
*[http://www.gnu.org/software/bash/bash.html  GNU Bash在GNU的主页]{{Wayback|url=http://www.gnu.org/software/bash/bash.html |date=20060813010211 }}&lt;br /&gt;
*[http://bashdb.sourceforge.net bashdb]{{Wayback|url=http://bashdb.sourceforge.net/ |date=20170602211310 }}，带有调试器的Bash&lt;br /&gt;
*[http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash入门指南]{{Wayback|url=http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html |date=20060808220934 }}{{en icon}}&lt;br /&gt;
*[http://markhobley.yi.org/linux/packages/shells/bash/bashdiff.html Bash同标准unix shell的不同]{{Wayback|url=http://markhobley.yi.org/linux/packages/shells/bash/bashdiff.html |date=20060719084754 }}&lt;br /&gt;
*[http://www-128.ibm.com/developerworks/library/l-bash.html Bash by example]{{Wayback|url=http://www-128.ibm.com/developerworks/library/l-bash.html |date=20060820205644 }} Bash基础编程&lt;br /&gt;
&lt;br /&gt;
{{-}}&lt;br /&gt;
{{GNU}}&lt;br /&gt;
{{程序设计语言}}&lt;br /&gt;
{{Unix Shells}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Unix shells]]&lt;br /&gt;
[[Category:脚本语言]]&lt;br /&gt;
[[Category:GNU计划软件]]&lt;br /&gt;
[[Category:面向文本编程语言]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Mhss</name></author>
	</entry>
</feed>