如何升级Bash

本文描述如何将Bash版本从5.1.8升级到最新的5.3。

💡 Bash 5.1.8 是系统默认安装版本,可以理解为是稳定和成熟的版本。本文只探讨升级过程,对于生产环境,不建议升级。

我的Linux环境为Oracle Linux 9.7, Bash版本5.1.8:

bash 复制代码
$ echo $MACHTYPE
x86_64-redhat-linux-gnu
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 9.7 (Plow)

$ bash --version
GNU bash, version 5.1.8(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

从安装包中查看当前Bash的版本信息:

bash 复制代码
$ yum list installed | grep bash
bash.x86_64                                    5.1.8-9.el9                           @System

$ yum info bash
Last metadata expiration check: 0:01:51 ago on Fri 16 Jan 2026 06:48:42 AM UTC.
Installed Packages
Name         : bash
Version      : 5.1.8
Release      : 9.el9
Architecture : x86_64
Size         : 7.4 M
Source       : bash-5.1.8-9.el9.src.rpm
Repository   : @System
Summary      : The GNU Bourne Again shell
URL          : https://www.gnu.org/software/bash
License      : GPLv3+
Description  : The GNU Bourne Again shell (Bash) is a shell or command language
             : interpreter that is compatible with the Bourne shell (sh). Bash
             : incorporates useful features from the Korn shell (ksh) and the C shell
             : (csh). Most sh scripts can be run by bash without modification.

Available Packages
Name         : bash
Version      : 5.1.8
Release      : 9.el9
Architecture : src
Size         : 10 M
Source       : None
Repository   : ol9_baseos_latest
Summary      : The GNU Bourne Again shell
URL          : https://www.gnu.org/software/bash
License      : GPLv3+
Description  : The GNU Bourne Again shell (Bash) is a shell or command language
             : interpreter that is compatible with the Bourne shell (sh). Bash
             : incorporates useful features from the Korn shell (ksh) and the C shell
             : (csh). Most sh scripts can be run by bash without modification.

试图自动升级:

复制代码
$ sudo yum update bash
Last metadata expiration check: 0:35:33 ago on Fri 16 Jan 2026 06:15:31 AM UTC.
Dependencies resolved.
Nothing to do.
Complete!

以上输出表示,基于已配置的官方repository,此bash已是最新版。

实际上5.1这个小版本中,最新的是5.1.16。系统认为5.1.8是最新,应该是处于稳定性的考虑。因为Bash是核心组件,官方通常不会通过 yum update 将其进行升级,最多只会推送同一个小版本系列的安全补丁。

由于不能自动升级,只能下载源码手工安装了。以下安装过程参考 GNU Bash手册 10.1 Basic Installation

目前Bash版本最新为5.3。首先从GNU官网下载Bash源码:

bash 复制代码
$ wget https://ftp.gnu.org/gnu/bash/bash-5.3.tar.gz
$ ls -lh --time-style=long-iso
total 11M
-rw-r--r--.  1 root root   11M 2025-07-30 14:07 bash-5.3.tar.gz

可以看到源码的日期为25年7月30日,大小11M。

解压,展开后近40M:

bash 复制代码
$ tar -zxf bash-5.3.tar.gz
$ ls
bash-5.3  bash-5.3.tar.gz
$ du -sh bash-5.3
39M     bash-5.3

然后进入标准的安装过程,中间的大量输出省略:

bash 复制代码
$ cd bash-5.3/
# configure 耗时 38秒
$ ./coechnfigure
# make 耗时1分12秒
$ make
# tests耗时2分10秒,报了很多警告,但$?仍是0
$ make tests
# install 耗时12秒
$ make install

整个过程不到5分钟,还是挺快的。没想到最慢的是下载源码。

💡 默认安装目录为 /usr/local/lib/bash,不会覆盖系统的 /bin/bash。

查看安装的版本:

bash 复制代码
$ /usr/local/bin/bash --version
GNU bash, version 5.3.0(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2025 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

重新登录终端,发现默认的路径已经指向新版本bash:

bash 复制代码
$ which bash
/usr/local/bin/bash
$ bash --version
GNU bash, version 5.3.0(1)-release (x86_64-pc-linux-gnu)
...

其实是因为PATH中,/usr/local/bin在/usr/bin之前(如果不是,就自己改一下):

bash 复制代码
$ echo $PATH
/home/vagrant/.local/bin:/home/vagrant/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

$ ls -lh --time-style=long-iso /usr/bin/bash
-rwxr-xr-x. 1 root root 1.4M 2024-05-01 05:59 /usr/bin/bash
$ ls -lh --time-style=long-iso /usr/local/bin/bash
-rwxr-xr-x. 1 root root 4.4M 2026-01-16 07:26 /usr/local/bin/bash

但是man page还是老版本的:

bash 复制代码
$ man -P cat bash | tail -1
GNU Bash 5.1                                   2020 October 29                                       BASH(1)
$ find / -name bash.1.gz
/usr/share/man/man1/bash.1.gz

就下来就有两种方式供选择,一个是新老Bash共存,一个是只想用新版本。

稳妥起见,建议新老版本共存。此时可以用符号链接和别名方式。

例如:

bash 复制代码
# cd /usr/bin
# ls -l bash*
-rwxr-xr-x. 1 root root 1389064 May  1  2024 bash
# ln -s /usr/local/bin/bash bash52
# ls -l bash*
-rwxr-xr-x. 1 root root 1389064 May  1  2024 bash
lrwxrwxrwx. 1 root root      19 Jan 16 08:25 bash52 -> /usr/local/bin/bash
# bash52
# echo $BASH_VERSION
5.3.0(1)-release

# echo $MANPATH
/usr/share/man:
# alias man52="MANPATH=/usr/local/share/man man"
# man52 -P cat bash| tail -1
GNU Bash 5.3                                                                  2025 April 7                                                                      BASH(1)

如果想用新版本彻底替代老版本,以下是一些标准的方法:

bash 复制代码
sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'
chsh -s /usr/local/bin/bash

我并没有执行,因为chsh还未安装。

不过以下给出了一种方式,例如只让某个用户(例如vagrant)使用最新的Bash:

bash 复制代码
# usermod -s /usr/local/bin/bash vagrant
# grep vagrant /etc/passwd
vagrant:x:1000:1000::/home/vagrant:/usr/local/bin/bash

其实就是把用户的login shell改了。用putty试了一下,可以的:

bash 复制代码
Using username "vagrant".
Authenticating with public key "vagrant"

Welcome to Oracle Linux Server release 9.5 (GNU/Linux 5.15.0-206.153.7.el8uek.x86_64)

The Oracle Linux End-User License Agreement can be viewed here:

  * /usr/share/eula/eula.en_US

For additional packages, updates, documentation and community help, see:

  * https://yum.oracle.com/

Last login: Fri Jan 16 07:57:30 2026 from 10.0.2.2
[vagrant@ol9-vagrant ~]$ echo $VERSION_INFO

[vagrant@ol9-vagrant ~]$ echo $BASH_VERSION
5.3.0(1)-release
相关推荐
ScilogyHunter8 小时前
Autotools:GNU构建系统的基石与遗产
make·构建系统
我是小鳄鱼1 天前
Day 3: Bash 工具-- 30天复刻了一个 Claude Code
开发语言·bash
源文雨1 天前
批量递归转换 mp4 为 flac/m4a 的 bash 脚本
开发语言·ffmpeg·bash·转码·mp4·m4a·flac
ScilogyHunter2 天前
CMake:现代C/C++项目的构建中枢
make·构建系统
想唱rap3 天前
表的约束条件
linux·数据库·mysql·ubuntu·bash
倔强的石头1063 天前
【Linux指南】进程控制系列(四)进程替换 ——exec 系列函数全解析与应用
linux·运维·bash
tianyuanwo4 天前
Bash与Sh的诞生背景、底层原理及Linux多Shell解释器兼容性解析
linux·开发语言·bash
木风小助理6 天前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
liliangcsdn8 天前
bash中awk如何切分输出
开发语言·bash