GNU/Bash 5.1 released with the random ${SRANDOM} number engine

Current GNU/Bash maintainer Chet Ramey announced the latest major release, Bash 5.1, on December 7th, 2020. Bash is the default shell on many Linux distros and popular among developers, especially those working with Linux containers. It is the fifth major release of bash. It came with a new enhanced random number generation and added a new variable called SRANDOM. Let us see how to install and use newly released GNU/BASH 5.1 on Linux.

create or generate random numbers (sequence of numbers that lack any pattern) under the Bash using the following:

echo "$RANDOM"
printf "%sn" "$RANDOM"

Here is the bash for loop to test random numbers:

for r in {1..5}
do printf "%sn" "$RANDOM"

A new bash variable, SRANDOM added to GNU/Bash-5.1 release, which gets its random data from the system’s entropy engine and is not lined and cannot be reseeded to get an identical random sequence. For instance:

printf "%sn" "$SRANDOM"
echo "$SRANDOM"
for r in {1..5}; do printf "%sn" "$SRANDOM"; done

GNU Bash 5.1 released with the SRANDOM number engine for Linux

GNU Bash 5.1 released with the SRANDOM number engine for Linux
This variable expands to a 32-bit pseudo-random number each time it is referenced. The random number generator is not linear on systems that support /dev/urandom or arc4random, so each returned number has no relationship to the numbers preceding it. The random number generator cannot be seeded, so assignments to this variable have no effect. If SRANDOM is unset, it loses its unique properties, even if it is subsequently reset.

New builtins and other features

  1. There are new loadable builtins mktemp, accept, mkfifo, csv, and cut/lcut.
  2. The shell now expands the history number (e.g., in PS1) even if it is not currently saving commands to the history list.
  3. select builtin now runs traps if its internal call to the read builtin is interrupted by a signal.
  4. Bash now allows SIGINT trap handlers to execute recursively.
  5. Process substitution is now available in posix mode.
  6. ulimit has a -R option to report and set the RLIMIT_RTTIME resource.
  7. Writing history to syslog now handles messages longer than the syslog maxlength by writing multiple messages with a sequence number.

See compete list here online

Installing GNU/Bash 5.1 on Linux and macOS

On Arch Linux all you have to do is type the following pacman command:
# pacman -Syu
macOS users change or install bash using the brew command. Make sure you install Homebrew on macOS and then type the following brew command:
$ brew install bash
Debian bullseye/sid users run the apt command to get the latest release:
$ sudo apt update
$ sudo apt upgrade

A note about source code installation

First install GNU/GCC compiler collectiona as per your OS:

Now grab the tarball using the wget command or download a file with curl:
$ wget
Untar or extract tar ball, run:
$ tar xvf bash-5.1.tar.gz
The simplest way to compile Bash is to cd into the source code directory using the cd command and then run:
$ cd bash-5.1
$ ./configure

unning ‘configure’ takes some time. While running, it prints messages telling which features it is checking for. Type ‘make’ to compile Bash and build the ‘bashbug’ bug reporting script too:
$ make
Optionally, type ‘make tests’ to run the Bash test suite and then type ‘make install’ to install ‘bash’ and ‘bashbug’. This will also install the manual pages and Info file:
$ sudo make install
Load your new bash and check your bash version:

bash --version

Wrapping up

Apart from enhanced random number generation and added a new variable called $SRANDOM in GNU/Bash 5.1, we see many other minor enhancements. Head over to the GNU bash project homepage to grab the latest software.

Posted by Contributor