下表列出了标准shell(sh), Bourne Again SHell (bash), Korn shell (ksh) 和 C shell (csh)之间的主要区别.
![]() |
Shell兼容性 |
---|---|
既然Bash是 sh 的超集,那么所有的 sh 命令将仍然能在bash里面工作,但是反之则不然。 bash 有很多自己的特性,而且,就像下表证明的,很多特性混合自其他shell。 既然Turbo C shell是 csh 的超集,那么所有的 csh 命令也能在 tcsh 里面工作,但是反之不然。 |
表 A.2. 不同shell的特性
sh | bash | ksh | csh | Meaning/Action |
---|---|---|---|---|
$ | $ | $ | % | 默认用户提示 |
>| | >| | >! | 强制重定向 | |
> file 2>&1 |
&> file 或者 > file 2>&1 |
> file 2>&1 |
>& file |
重定向标准输出和标准错误到 文件 |
{ } | { } | 扩展列表中的元素 | ||
`command` | `command` or $(command) | $(command) | `command` | Substitute output of enclosed command |
$HOME |
$HOME |
$HOME |
$home |
Home目录 |
~ | ~ | ~ | Home目录符号 | |
~+ , ~- , dirs |
~+ , ~- |
=- , =N |
访问目录栈 | |
var =value |
VAR =value |
var =value |
set var =value |
变量赋值 |
export var |
export VAR =value |
export var =val |
setenv var val |
设置环境变量 |
${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 | 打印工作目录 |
read | read | read | $< | Read from terminal |
trap 2 |
trap 2 |
trap 2 |
onintr | 忽略中断 |
unalias | unalias | unalias | 去除别名 | |
until | until | until | 开始 until 循环 | |
while/do | while/do | while/do | while | 开始 while 循环 |
Bash还有很多这里没有列出的特性。这个表 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.
更多信息:
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.