FreeBSD Install BASH Shell Using pkg command

I would like to Bash shell under FreeBSD for bash programming. How do I install bash shell under FreeBSD UNIX operating system?

Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. csh / tcsh (C shell with file name completion and command line editing) is the default shell under FreeBSD. Bash is a much better shell as compared to csh or tcsh. Hence, I recommend installing it. You can easily install and use bash as shell on FreeBSD based VPS or cloud server. This page shows how to install bash shell on FreeBSD operating system.

ADVERTISEMENTS

Update your FreeBSD system

Run the following pkg command to update all install packages:
# pkg update
# pkg upgrade

Sample session:

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking for upgrades (3 candidates): 100%
Processing candidates (3 candidates): 100%
The following 2 package(s) will be affected (of 0 checked):
 
Installed packages to be UPGRADED:
	ca_root_nss: 3.40.1 -> 3.45
	amazon-ssm-agent: 2.0.790.0_1 -> 2.3.612.0
 
Number of packages to be upgraded: 2
 
The process will require 66 MiB more space.
18 MiB to be downloaded.
 
Proceed with this action? [y/N]: y

FreeBSD install Bash SHELL

Login as root and type any one of the following commands to install bash. On a newer version of FreeBSD use the following command (recommended method):
# pkg install bash
Sample outputs:

Fig.01: Howto Install BASH in FreeBSD using pkg Command

Fig.01: Howto Install BASH in FreeBSD using pkg Command

Install Bash shell using pkg_add

To install FreeBSD binary package, enter (older version/method):
# pkg_add -r bash

How to Install Bash Shell on FreeBSD using ports

To install the bash shell port, run:
# portsnap update extract
# cd /usr/ports/shells/bash
# make install clean

How do I use bash shell under my FreeBSD based system?

To use bash shell, enter:
# bash
OR
# /usr/local/bin/bash

Configuration

The GNU bash under FreeBSD 11.x and older requires fdescfs (file-descriptor file system) mounted on /dev/fd. To mount it auotmatically type the following command to update /etc/fstab file on FreeBSD:

## append the following to /etc/fstab file under FreeBSD 11.x and older ##
echo "fdesc  /dev/fd  fdescfs  rw 0 0" >> /etc/fstab

How do I set bash as a default shell?

To set bash as default, shell enter:
# chsh -s /usr/local/bin/bash {username}
# chsh -s /usr/local/bin/bash vivek
# chsh -s bash

Verify that bash is default shell for a user account called vivek using grep command/egrep command, enter:
grep vivek /etc/passwd
OR
egrep --color 'vivek|bas' /etc/passwd
OR
finger vivek
Sample outputs:

Login: vivek          			Name: Vivek Gite
Directory: /home/vivek              	Shell: /usr/local/bin/bash
On since Tue May  8 02:15 (IST) on pts/0 from 192.168.1.5
No Mail.
No Plan.

How to install bash completion on FreeBSD

Type the following pkg command:
# pkg install bash-completion

How to Install bash auto completion on FreeBSD

How to Install bash auto completion on FreeBSD

To enable the bash completion library, add the following to your ~/.bashrc or ~/.bash_profile file:

[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && \
	. /usr/local/share/bash-completion/bash_completion.sh

Log out and log in again to test new settings.

Conclusion

And there you have it, bash shell installed and working on FreeBSD. However, for root user use the default /bin/csh for security reasons. Do not change /bin/csh to /usr/bin/local/bash for root user. See my bash shell scripting tutorial for more info or GNU/bash homepage here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
9 comments… add one
  • duzap May 22, 2009 @ 8:12

    This was very usefull for me, thank you.

  • matt Nov 22, 2009 @ 18:43

    Thanks,

    Just trying out freebsd, and the first thing I noticed was there was no bash!

  • Nazar Aug 4, 2010 @ 1:00

    Thanks a lot! Very clearly written and useful!

  • DJNeo Aug 5, 2010 @ 2:14

    Very usefull,10x alot!!!

  • George Dec 14, 2010 @ 12:09

    Thanks a lot! Exactly what I needed!

  • Chuck Moyes Mar 22, 2011 @ 20:50

    I think the portsnap part (updating the system port tree) needs clarification.

    From the FreeBSD Handbook:

    First run:
    # portsnap fetch
    to download a new port tree from the Internet.

    If you haven’t run portsnap before (first time updating after new installation):
    # portsnap extract

    Then always run:
    # portsnap update

    FreeBSD complained about running update before extract when I tried doing what the guide on this web page said to do. Let me know if I’m misinterpreting something.

  • Rui Mar 28, 2011 @ 22:46

    Thank you!!!:)

  • Alex Jun 10, 2012 @ 10:27

    Thanks a lot! Just used it. =)

  • Uwe Sep 24, 2012 @ 20:25

    Thank you for this post. It was very useful for me!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.