转自 http://www.cnblogs.com/zmlctt/p/3721022.html
一、实践证明,在AIX上用的是ksh,linux上是bash
sh或bsh,全名是bourne shell。它最早出现,是标准shell。后两者都兼容它。
ksh和bash后续加入了历史记录,交互特性,数组,等新功能。
ksh在unix上使用较多。比如hpux,AIX
bash在linux上使用较多。
也可以用这些做命令跳到另一个shell,看个人习惯用哪个:
jimmy-cao@jimmycao-ThinkPad-Edge:~$ sh
$ bash
jimmy-cao@jimmycao-ThinkPad-Edge:~$ ksh
$
bash可以使用tab键补全,ksh貌似不能
二、使用上的具体区别,转的
(2) ksh与bash初始化数组的语法不同:
如下所示
icymoon# ksh
icymoon# set -A array 1 2 3 4 5; echo ${array[3]};
4
icymoon# bash
icymoon# array=(1 2 3 4 5); echo ${array[3]};
4
icymoon# array=([0]=1 [3]=2 [2]=3); echo ${array[3]}
2
read VARIABLE?\ "xxxxxxxxxxxxxx"
2. 反义循环
until [[ ... ]] #这里的[[ ]]是ksh的括号,比sh的[]括号,[[]]能够支持更多,更强大的命令行选项
do
xxxxxxx
done
2. 字符串的操作
我们有basename取出文件名,但是如果需要得到目录名呢?
FULLPATH=`pwd`/$0
DIR=${FULLPATH%${FILENAME}}#从变量FULLPATH的最后开始,搜索第一个FILENAME的匹配,并删除
%% 后面 最后一个
# 前面 第一个
## 后面 最后一个
DIR=${DIR%\/} #去掉反斜杠
basename=$(filename%%.cpp) #去掉文件名的.cpp后缀
3. 布尔操作
sh的布尔操作是-a -o,ksh可以 if [[ test1 && test2 ]]就像C语言的逻辑操作一样
4. 把一个字符串拆成一个数组,元素之间是以空格或tab分隔
set -A Array $input
if [[ -z ${Array[0]} ]]echo"空的数组"
取数组下标和C语言类似,当然必须用${}包含
5. for数据(sh兼容)
for file in $inputfiles
do
cat $file|tr '[A-Z]' '[a-z]'|sed -e '/^$/d' -e '/^#/d'
#所有字符变成小写,并删除空行和注释行('#'开头)
done
6. 寻找新的文件 NEWER=`find $CLASSFILE -newer $JAVAFILE`
7. 直接数学计算
$ let x=0
$ let x=x+2
不需要使用expr反复计算
下表转自: http://www.tldp.org/LDP/Bash-Beginners-Guide/html/x7369.html
A.2. Differing features
The table below shows major differences between the standard shell (sh), Bourne Again SHell (bash), Korn shell (ksh) and the C shell (csh).
![]() | Shell compatibility |
---|---|
Since the Bourne Again SHell is a superset of sh, all sh commands will also work in bash - but not vice versa. bash has many more features of its own, and, as the table below demonstrates, many features incorporated from other shells. Since the Turbo C shell is a superset of csh, all csh commands will work in tcsh, but not the other way round. |
Table A-2. Differing Shell Features
sh | bash | ksh | csh | Meaning/Action |
---|---|---|---|---|
$ | $ | $ | % | Default user prompt |
>| | >| | >! | Force redirection | |
> file 2>&1 | &> file or > file 2>&1 | > file 2>&1 | >& file | Redirect stdout and stderr to file |
{ } | { } | Expand elements in list | ||
`command` | `command` or $(command) | $(command) | `command` | Substitute output of enclosed command |
$HOME | $HOME | $HOME | $home | Home directory |
~ | ~ | ~ | Home directory symbol | |
~+, ~-, dirs | ~+, ~- | =-, =N | Access directory stack | |
var=value | VAR=value | var=value | set var=value | Variable assignment |
export var | export VAR=value | export var=val | setenv var val | Set environment variable |
${nnnn} | ${nn} | More than 9 arguments can be referenced | ||
"$@" | "$@" | "$@" | All arguments as separate words | |
$# | $# | $# | $#argv | Number of arguments |
$? | $? | $? | $status | Exit status of the most recently executed command |
$! | $! | $! | PID of most recently backgrounded process | |
$- | $- | $- | Current options | |
. file | source file or . file | . file | source file | Read commands in file |
alias x='y' | alias x=y | alias x y | Name x stands for command y | |
case | case | case | switch or case | Choose alternatives |
done | done | done | end | End a loop statement |
esac | esac | esac | endsw | End case or switch |
exit n | exit n | exit n | exit (expr) | Exit with a status |
for/do | for/do | for/do | foreach | Loop through variables |
set -f, set -o nullglob|dotglob|nocaseglob|noglob | noglob | Ignore substitution characters for filename generation | ||
hash | hash | alias -t | hashstat | Display hashed commands (tracked aliases) |
hash cmds | hash cmds | alias -t cmds | rehash | Remember command locations |
hash -r | hash -r | unhash | Forget command locations | |
history | history | history | List previous commands | |
ArrowUp+Enter or !! | r | !! | Redo previous command | |
!str | r str | !str | Redo last command that starts with "str" | |
!cmd:s/x/y/ | r x=y cmd | !cmd:s/x/y/ | Replace "x" with "y" in most recent command starting with "cmd", then execute. | |
if [ $i -eq 5 ] | if [ $i -eq 5 ] | if ((i==5)) | if ($i==5) | Sample condition test |
fi | fi | fi | endif | End if statement |
ulimit | ulimit | ulimit | limit | Set resource limits |
pwd | pwd | pwd | dirs | Print working directory |
read | read | read | $< | Read from terminal |
trap 2 | trap 2 | trap 2 | onintr | Ignore interrupts |
unalias | unalias | unalias | Remove aliases | |
until | until | until | Begin until loop | |
while/do | while/do | while/do | while | Begin while loop |
The Bourne Again SHell has many more features not listed here. This table is just to give you an idea of how this shell incorporates all useful ideas from other shells: there are no blanks in the column for bash. More information on features found only in Bash can be retrieved from the Bash info pages, in the "Bash Features" section.
More information:
You should at least read one manual, being the manual of your shell. The preferred choice would be info bash, bash being the GNU shell and easiest for beginners. Print it out and take it home, study it whenever you have 5 minutes.