Back to Shell_Script

See Also ZshUnix Utilities

bash

bash是Unix shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写。1989年发布第一个正式版本,原先是计划用在GNU操作系统上,但能运行于大多数类Unix系统的操作系统之上,包括Linux与Mac OS X v10.4都将它作为默认shell。它也被移植到Microsoft Windows上的Cygwin与MinGW,或是可以在MS-DOS上使用的DJGPP项目。在Novell NetWare与Andriod在上也有移植。1990年后,Chet Ramey成为了主要的维护者。

bash为Bourne shell的后继兼容版本与开放源代码版本,它的名称来自Bourne shell(sh)的一个双关语(Bourne again / born again):Bourne-Again SHell。

1. 日常使用

1.1. 学习教程

1.2. Bash的启动过程

1.3. 基础配置

Refer to http://pkill-9.com/enabling-bash_aliases/

# vi ~/.bash_profile
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

export MAVEN_HOME=/opt/local/tools/java/maven/apache-maven-2.2.1

#export LC_ALL=zh_CN.UTF-8
export PATH=/usr/local/bin:/usr/local/sbin:$PATH:$MAVEN_HOME/bin:~/app/bin
export LC_ALL=en_US.UTF-8
export eclipse_base=/opt/local/ide/eclipses
export JAVA_HOME=/Library/Java/Home
function ff { osascript -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "get POSIX path of (target of window ${1-1} as alias)"\
 -e 'else' -e 'get POSIX path of (desktop as alias)'\
 -e 'end if' -e 'end tell'; };\

function cdff { cd "`ff $@`"; };

# vi ~/.bash_aliases
alias ll='ls -alF'
alias qlf='qlmanage -p'


1.4. Bash Shell Keyboard Shortcuts

Shell_Script#Bash_Shell_Keyboard_Shortcuts

2. 语法

2.1. 支持的功能

# Mac OS X
➜  ~ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
# Ubuntu
$ bash --version
GNU bash, version 4.2.37(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.

2.1.1. Associative Arrays(need V4.x+)

declare -A a         # declare an associative array 'a' faking a bi-dimensional indexed array
i=1; j=2             # initialize some indices
a[$i,$j]=5           # associate value "5" to key "$i,$j" (i.e. "1,2")
echo ${a[$i,$j]}     # print the stored value at key "$i,$j"

2.1.2. Brace Expansion

echo a{p,c,d,b}e       # ape ace ade abe
echo {a,b,c}{d,e,f}    # ad ae af bd be bf cd ce cf
ls *.{jpg,jpeg,png}    # expands to *.jpg *.jpeg *.png - after which,
                       # the wildcards are processed

3. Others

3.1. Security updates for Shellshock

昨天网络上曝出一个bash存在的漏洞,并标识为CVE-2014-6271;因为该漏洞可被远程利用,因此命名为Shellshock,会影响到Debian/CentOS等Linux发行版,安全专家建议尽快修补。

3.1.1. 检查是否需要修补

$ env x='() { :;}; echo vulnerable' bash -c 'echo hello'
# 没有漏洞
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
hello
# 存在漏洞
vulnerable
hello

3.1.2. 对不再进行系统支持的Ubuntu版本的升级

参考How do I update bash on a Quantal system?上的建议,下载源码和补丁文件,重新编译和安装,完成了修复。

cd /tmp
mkdir bash
cd bash
wget http://ftp.gnu.org/gnu/bash/bash-4.3.tar.gz
#download all patches
for i in $(seq -f "%03g" 0 25); do wget http://ftp.gnu.org/gnu/bash/bash-4.3-patches/bash43-$i; done
tar zxvf bash-4.3.tar.gz 
cd bash-4.3
#apply all patches
for i in $(seq -f "%03g" 0 25);do patch -p0 < ../bash43-$i; done
#build and install
./configure && make
sudo make install

脚本来自:http://askubuntu.com/questions/528101/what-is-the-cve-2014-6271-bash-vulnerability-and-how-do-i-fix-it

重新登录后:

$ bash --version
GNU bash, version 4.3.25(1)-release (i686-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
this is a test

4. Reference


CategorymacOS CategoryShell

MainWiki: bash (last edited 2014-09-26 16:32:32 by twotwo)