Linux下sh與bash的區別

在學習shell腳本的過程中發現一個問題,使用./與sh 執行腳本的結果不同,如下:

#!/bin/bash                                                                     
read -p "please input: " tmp    
echo ${tmp}
[ "${tmp}" == "Y" ] || [ "${tmp}" == "y" ] && echo -e "OK,continue" && exit 0
[ "${tmp}" == "N" ] || [ "${tmp}" == "n" ] && echo -e "Oh,interrrupt" && exit 0
echo -e "I dont't know what you choice is" && exit 0

很是費解,明明在腳本中都聲明瞭調用/bin/bash了,怎麼結果還不同呢?於是查找資料,總結如下。


shell簡介

第一個流行的shell是由Steven Bourne 發展出來的,爲了紀念它就稱爲Bourne shell,或直接簡稱爲sh

而Linux使用的版本爲Bourne Again SHell 簡稱爲bash,這個shell是Bourne shell 的增強版本

爲什麼bash與sh的不同

在Ubuntu中我們可以看到

我們打開man bash  可以看到

如果使用sh名稱調用bash,它將嘗試模擬啓動儘可能接近歷史版本sh的行爲,而且符合POSIX標準。

dash是什麼呢

從Ubuntu 6.10開始,默認使用dash(theDebian Almquist Shell)而不是bash(the GNUBourne-Again Shell). 但Login Shell還是bash. 原因是dash更快、更高效,而且它符合POSIX規範。Ubuntu在啓動的時候會運行很多shell腳本,使用dash可以加快啓動速度。

所以說,sh就是一個低版本的 bash,它有一些功能不完善,我們需要注意儘量使用bash。

bash與sh的不同之處

1.bash 中的echo -e 可以啓用反斜槓的轉義

   sh中沒有。

2.以後用到再翻譯。。。


bash與sh不同的原文

Things bash has that sh does not:

   long invocation options

   [+-]O invocation option

   -l invocation option

   `!' reserved word to invert pipeline return value

   `time' reserved word to time pipelines and shell builtins

   the `function' reserved word

   the `select' compound command and reserved word

   arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done

   new $'...' and $"..." quoting

   the $(...) form of command substitution

   the $(<filename) form of command substitution, equivalent to

      $(cat filename)

   the ${#param} parameter value length operator

   the ${!param} indirect parameter expansion operator

   the ${!param*} prefix expansion operator

   the ${param:offset[:length]} parameter substring operator

   the ${param/pat[/string]} parameter pattern substitution operator

   expansions to perform substring removal (${p%[%]w}, ${p#[#]w})

   expansion of positional parameters beyond $9 with ${num}

   variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY,

         TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS,

         LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME,

         ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE,

         HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,

         PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,

         SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars,

         auto_resume

   DEBUG trap

   ERR trap

   variable arrays with new compound assignment syntax

   redirections: <>, &>, >|, <<<, [n]<&word-, [n]>&word-

   prompt string special char translation and variable expansion

   auto-export of variables in initial environment

   command search finds functions before builtins

   bash return builtin will exit a file sourced with `.'

   builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -d/-l/-p/-t.

        export -n/-f/-p/name=value, pwd -L/-P,

        read -e/-p/-a/-t/-n/-d/-s/-u,

        readonly -a/-f/name=value, trap -l, set +o,

        set -b/-m/-o option/-h/-p/-B/-C/-H/-P,

        unset -f/-v, ulimit -i/-m/-p/-q/-u/-x,

        type -a/-p/-t/-f/-P, suspend -f, kill -n,

        test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S

   bash reads ~/.bashrc for interactive shells, $ENV for non-interactive

   bash restricted shell mode is more extensive

   bash allows functions and variables with the same name

   brace expansion

   tilde expansion

   arithmetic expansion with $((...)) and `let' builtin

   the `[[...]]' extended conditional command

   process substitution

   aliases and alias/unalias builtins

   local variables in functions and `local' builtin

   readline and command-line editing with programmable completion

   command history and history/fc builtins

   csh-like history expansion

   other new bash builtins: bind, command, compgen, complete, builtin,

             declare/typeset, dirs, enable, fc, help,

             history, logout, popd, pushd, disown, shopt,

             printf

   exported functions

   filename generation when using output redirection (command >a*)

   POSIX.2-style globbing character classes

   POSIX.2-style globbing equivalence classes

   POSIX.2-style globbing collating symbols

   egrep-like extended pattern matching operators

   case-insensitive pattern matching and globbing

   variable assignments preceding commands affect only that command,

      even for builtins and functions

   posix mode and strict posix conformance

   redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr,

      /dev/tcp/host/port, /dev/udp/host/port

   debugger support, including `caller' builtin and new variables

   RETURN trap

   the `+=' assignment operator

Things sh has that bash does not:

   uses variable SHACCT to do shell accounting

   includes `stop' builtin (bash can use alias stop='kill -s STOP')

   `newgrp' builtin

   turns on job control if called as `jsh'

   $TIMEOUT (like bash $TMOUT)

   `^' is a synonym for `|'

   new SVR4.2 sh builtins: mldmode, priv

Implementation differences:

   redirection to/from compound commands causes sh to create a subshell

   bash does not allow unbalanced quotes; sh silently inserts them at EOF

   bash does not mess with signal 11

   sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100

   bash splits only the results of expansions on IFS, using POSIX.2

      field splitting rules; sh splits all words on IFS

   sh does not allow MAILCHECK to be unset (?)

   sh does not allow traps on SIGALRM or SIGCHLD

   bash allows multiple option arguments when invoked (e.g. -x -v);

      sh allows only a single option argument (`sh -x -v' attempts

      to open a file named `-v', and, on SunOS 4.1.4, dumps core.

      On Solaris 2.4 and earlier versions, sh goes into an infinite

      loop.)

   sh exits a script if any builtin fails; bash exits only if one of

      the POSIX.2 `special' builtins fails

發佈了63 篇原創文章 · 獲贊 71 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章