Linux shell ---ksh bash 区别

转自 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貌似不能

二、使用上的具体区别,转的

(1) 在ksh是,数组的index只能从0到1023,而bash中没有这样的限制。
(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
 
1. 内置read命令
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).

NoteShell 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

shbashkshcshMeaning/Action
$$$%Default user prompt
 >|>|>!Force redirection
> file 2>&1&> file or > file 2>&1> file 2>&1>& fileRedirect stdout and stderr to file
 { } { }Expand elements in list
`command``command` or $(command)$(command)`command`Substitute output of enclosed command
$HOME$HOME$HOME$homeHome directory
 ~~~Home directory symbol
 ~+, ~-, dirs~+, ~-=-, =NAccess directory stack
var=valueVAR=valuevar=valueset var=valueVariable assignment
export varexport VAR=valueexport var=valsetenv var valSet environment variable
 ${nnnn}${nn} More than 9 arguments can be referenced
"$@""$@""$@" All arguments as separate words
$#$#$#$#argvNumber of arguments
$?$?$?$statusExit status of the most recently executed command
$!$!$! PID of most recently backgrounded process
$-$-$- Current options
. filesource file or . file. filesource fileRead commands in file
 alias x='y'alias x=yalias x yName x stands for command y
casecasecaseswitch or caseChoose alternatives
donedonedoneendEnd a loop statement
esacesacesacendswEnd case or switch
exit nexit nexit nexit (expr)Exit with a status
for/dofor/dofor/doforeachLoop through variables
 set -f, set -o nullglob|dotglob|nocaseglob|noglob noglobIgnore substitution characters for filename generation
hashhashalias -thashstatDisplay hashed commands (tracked aliases)
hash cmdshash cmdsalias -t cmdsrehashRemember command locations
hash -rhash -r unhashForget command locations
 historyhistoryhistoryList previous commands
 ArrowUp+Enter or !!r!!Redo previous command
 !strr str!strRedo 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
fififiendifEnd if statement
ulimitulimitulimitlimitSet resource limits
pwdpwdpwddirsPrint working directory
readreadread$<Read from terminal
trap 2trap 2trap 2onintrIgnore interrupts
 unaliasunaliasunaliasRemove aliases
untiluntiluntil Begin until loop
while/dowhile/dowhile/dowhileBegin 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.




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值