Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh)、C shell (csh) 和 Korn shell (ksh), 各有优缺点。Bourne shell 是 UNIX 最初使用的 shell,并且在每种 UNIX 上都可以使用, 在 shell 编程方面相当优秀,但在处理与用户的交互方面做得不如其他几种shell。Linux 操作系统缺省的 shell 是Bourne Again shell,它是 Bourne shell 的扩展,简称 Bash,与 Bourne shell 完全向后兼容,并且在Bourne shell 的基础上增加、增强了很多特性。Bash放在/bin/bash中,它有许多特色,可以提供如命令补全、命令编辑和命令历史表等功能,它还包含了很多 C shell 和 Korn shell 中的优点,有灵活和强大的编程接口,同时又有很友好的用户界面。

    GNU/Linux 操作系统中的 /bin/sh 本是 bash (Bourne-Again Shell) 的符号链接,但鉴于 bash 过于复杂,有人把 ash 从 NetBSD 移植到 Linux 并更名为 dash (Debian Almquist Shell),并建议将 /bin/sh 指向它,以获得更快的脚本执行速度。Dash Shell 比 Bash Shell 小的多,符合POSIX标准。
    Ubuntu继承了Debian,所以从Ubuntu 6.10开始默认是Dash Shell。
  1.  luotaijia@ubuntu:~$ ls -l /bin/sh /bin/bash
  2.  -rwxr-xr-x 1 root root 801808 2010-08-11 03:58 /bin/bash
  3.  lrwxrwxrwx 1 root root 4 2012-11-28 08:06 /bin/sh -> dash
    应该说, /bin/sh 与 /bin/bash 虽然大体上没什么区别, 但仍存在不同的标准. 标记为 “#!/bin/sh” 的脚本不应使用任何 POSIX 没有规定的特性 (如 let 等命令, 但 “#!/bin/bash” 可以). Debian 曾经采用 /bin/bash 更改 /bin/dash,目的使用更少的磁盘空间、提供较少的功能、获取更快的速度。但是后来经过 shell 脚本测试存在运行问题。因为原先在 bash shell 下可以运行的 shell script (shell 脚本),在 /bin/sh 下还是会出现一些意想不到的问题,不是100%的兼用。

点击(此处)折叠或打开

  1. 1 a=12345
  2. 2
  3. 3 let “a += 1”
  4. 4 echo “a = $a”
  5. 5
  6. 6 b=${a/23/BB}
  7. 7 echo “b = $b”

点击(此处)折叠或打开

  1. luotaijia@ubuntu:~/文档/shell学习练习$ /bin/sh 3.2..1.sh
  2. 3.2..1.sh: 3: let: not found
  3. a = 12345
  4. 3.2..1.sh: 6: Bad substitution
  5. luotaijia@ubuntu:~/文档/shell学习练习$ /bin/bash 3.2..1.sh
  6. a = 12346
  7. b = 1BB46
  8. luotaijia@ubuntu:~/文档/shell学习练习$
    注: b=${a/23/BB} 把变量a中的23(仅限第一次出现)替换成BB, 并赋值给 b.

Leave a Reply

Your email address will not be published. Required fields are marked *