FreeBSD Install BASH Shell Using pkg command

last updated in Categories , , , ,

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.

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

To install the bash shell port:
# 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 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:

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 9 comments/add one below):

Join the discussion at www.nixcraft.com

Historical Comment Archive

9 comment

  1. 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.

    Have a question? Post it on our forum!