Ubuntu /bin/sh /bin/bash /bin/dash 区别

从Ubuntu 6.10开始,默认的系统shell /bin/sh由dash(Debian Almquist Shell)取代了bash(GNU Bourne-Again Shell)。本文解释了这一变化的原因,并提供了遇到问题时的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘自DashAsBinSh

Dash as /bin/sh

In Ubuntu 6.10, the default system shell, /bin/sh, was changed to dash (the Debian Almquist Shell); previously it had been bash (the GNU Bourne-Again Shell). The same change will affect users of Ubuntu 6.06 LTS upgrading directly to Ubuntu 8.04 LTS. This document explains this change and what you should do if you encounter problems.

The default login shell remains bash. Opening a terminal from the menu or shortcut [crtl-alt-t] provides interactive bash. A script run from the desktop or file manager, through the dialogue 'run in terminal' will execute as POSIX dash.

Why was this change made?

The major reason to switch the default shell was efficiency. bash is an excellent full-featured shell appropriate for interactive use; indeed, it is still the default login shell. However, it is rather large and slow to start up and operate by comparison with dash. A large number of shell instances are started as part of the Ubuntu boot process. Rather than change each of them individually to run explicitly under /bin/dash, a change which would require significant ongoing maintenance and which would be liable to regress if not paid close attention, the Ubuntu core development team felt that it was best simply to change the default shell. The boot speed improvements in Ubuntu 6.10 were often incorrectly attributed to Upstart, which is a fine platform for future development of the init system but in Ubuntu 6.10 was primarily running in System V compatibility mode with only small behavioural changes. These improvements were in fact largely due to the changed /bin/sh.

The Debian policy manual has long mandated that "shell scripts specifying '/bin/sh' as interpreter must only use POSIX features"; in fact, this requirement has been in place since well before the inception of the Ubuntu project. Furthermore, any shell scripts that expected to be portable to other Unix systems, such as the BSDs or Solaris, already honoured this requirement. Thus, we felt that the compatibility impact of this change would be minimal.

Of course, there have been a certain number of shell scripts written specifically for Linux systems, some of which incorrectly stated that they could run with /bin/sh when in fact they required /bin/bash, and these scripts will have broken due to this change. We regret this breakage, but feel that the proper way to address it is to make the small changes required to those scripts, discussed later in this document. In the longer term, this will promote a cleaner and more efficient system.

(This applies the same philosophy as in C and C++. Programs should be written to the standard, and if they use extensions they should declare them; that way it is clear what extensions are in use and they will at least fail with a much better error message if those extensions are not available.)

My production system has broken and I just want to get it back up!

If you are unlucky enough to have been negatively affected by this change, and only one or two shell scripts are affected, then the quickest way to fix this is to edit these scripts and change the first line to use the correct interpreter. The first line should look something like this (perhaps with some additional options):

 #! /bin/sh

Change that to the following (you can preserve any options you see):

 #! /bin/bash

In Makefiles, you can set the following variable at the top:

SHELL = /bin/bash

If the problems are more widespread and you want to change the default system shell back, then you can instruct the package management system to stop installing dash as /bin/sh:

sudo dpkg-reconfigure dash

Beware that this is a more invasive change, will undo boot speed benefits, and there is even an outside chance that there are a few scripts that now depend on some feature of dash that bash does not provide! (We expect the last problem to be rare, as the feature set of dash is largely a subset of that offered bybash, but we mention it for completeness.)

<think>我们正在处理用户关于更改/bin/sh默认链接到bash的问题。根据用户需求,需要将/bin/sh的符号链接指向bash。根据引用[2],我们知道在一般的Linux系统中,/bin/sh通常是bash的软链接,但有时可能指向其他shell(如dash)。用户希望将其重新指向bash。步骤:1.首先,确认当前/bin/sh指向什么。可以使用命令:`ls-l/bin/sh`2.如果当前指向的不是bash(例如指向dash),则需要更改。3.更改符号链接:使用`ln`命令。由于/bin/sh可能已经存在,我们需要先删除旧的链接,然后创建新的链接,或者使用`ln`的强制覆盖选项。具体命令:```bashsudoln-sf/bin/bash/bin/sh```这里:-`-s`表示创建符号链接(软链接)-`-f`表示强制覆盖已存在的目标文件(即如果/bin/sh已存在,则覆盖它)-第一个参数`/bin/bash`是源文件(目标),第二个参数`/bin/sh`是链接文件注意:使用sudo因为/bin/sh通常需要root权限才能修改。但是,根据引用[3]中给出的命令,他们使用了两个步骤:```bashln-f-s/bin/sh/bin/bash#这个命令看起来有误,因为它将sh链接到bash,然后又想将sh指向bash,实际上这里写反了ln-f-s/bin/bash/bin/sh#这才是正确的```实际上,用户只需要一条命令:`sudoln-sf/bin/bash/bin/sh`然而,引用[3]中的第一条命令`ln-f-s/bin/sh/bin/bash`是将/bin/bash链接到/bin/sh,这会导致bash被链接到sh,但通常我们不会这么做,因为bash本身是一个独立的程序。所以第二条命令才是我们需要的。因此,我们只需要执行:```bashsudoln-sf/bin/bash/bin/sh```验证:执行`ls-l/bin/sh`,应该看到类似:```lrwxrwxrwx1rootroot9日期/bin/sh->/bin/bash```警告:有些系统(如Debian/Ubuntu)默认使用dash作为/bin/sh,因为dash更轻量且符合POSIX,更改后可能会影响一些系统脚本(如果它们依赖于dash的行为)。但在大多数情况下,bash的POSIX模式(即作为sh时)是兼容的。如果遇到问题,可以改回dash。如果用户需要改回dash,可以使用:```bashsudoln-sf/bin/dash/bin/sh```根据引用[2]:sh是开启了POSIX标准的bash,所以理论上使用bash作为sh是可行的。最后,生成相关问题。</think>### 更改 /bin/sh 默认链接到 bash 的方法 要将 `/bin/sh` 的默认符号链接指向 `/bin/bash`,请按照以下步骤操作: 1. **查看当前链接状态**(确认当前指向): ```bash ls -l /bin/sh ``` 输出示例: ``` lrwxrwxrwx 1 root root 4 Jan 1 00:00 /bin/sh -> dash # 当前指向 dash ``` 2. **强制更新符号链接**: ```bash sudo ln -sf /bin/bash /bin/sh ``` - `-s`:创建符号链接 - `-f`:强制覆盖现有链接 - `/bin/bash`:目标解释器路径 - `/bin/sh`:要修改的链接路径 3. **验证修改结果**: ```bash ls -l /bin/sh ``` 成功输出应显示: ``` lrwxrwxrwx 1 root root 9 Jan 1 00:00 /bin/sh -> /bin/bash ✅ ``` ### 注意事项 1. **权限要求**:必须使用 `sudo` 获取 root 权限[^1] 2. **系统兼容性**:此操作会使系统脚本使用 Bash 的 POSIX 模式(相当于 `bash --posix`),多数脚本兼容但少数可能需特定 dash 行为[^2] 3. **恢复默认**:如需还原到 dash: ```bash sudo ln -sf /bin/dash /bin/sh ``` ### 技术说明 - `/bin/sh` 是系统级符号链接,指向默认 POSIX shell - Bash 作为 `sh` 时会自动启用 POSIX 兼容模式[^2] - Debian/Ubuntu 等系统默认链接到 dash 以提升性能[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值