Skip to content

How to Enable Bash Completion on macOS

Steps
5
Category
macOS
Versions & support
About macOS →
On this page 5 steps

Bash completion is a useful functionality that allows you to auto-complete commands, filenames, and arguments within a bash shell. This saves time, increases efficiency, and enhances the user experience when working in a terminal environment.

The latest version of macOS uses Zsh as its default shell, having switched from Bash with the Catalina release in 2019. However, if you still prefer to use Bash and wish to enable bash completion, here’s a comprehensive guide to help you.

Step 1 of 5Install Homebrew

Homebrew is a popular package manager for macOS that simplifies the installation of software on Apple’s macOS operating system and Linux. If you haven’t already installed Homebrew, you’ll need to do so to proceed.

To install Homebrew, open a terminal and execute the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 

Step 2 of 5Install Bash-Completion

Once Homebrew is installed on your macOS, you can use it to install bash-completion. This can be done by executing the following command:

brew install bash-completion 

Please note that if you’re using Bash 3.2 (which is the default on macOS), you should use bash-completion. However, if you have upgraded to Bash 4.1 or newer, you should use bash-completion@2 instead:

brew install bash-completion@2 

Step 3 of 5Enable Bash Completion

The above step only installs bash completion. The next step is to enable it by adding it to your .bash_profile or .bashrc file.

  • If you’re using bash-completion, add the following lines to your .bash_profile or .bashrc:
    
    if [ -f $(brew --prefix)/etc/bash_completion ]; then
      . $(brew --prefix)/etc/bash_completion
    fi
    
    
  • If you’re using bash-completion@2, add these lines:
    
    export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
    [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
    
    

You can use a text editor like nano or vi to add these lines. For example, to add them using nano, you would type:

nano ~/.bash_profile 

Then you can paste the lines at the end of the file, save the changes and close the editor (with nano, you can do this by pressing CTRL+O to save and CTRL+X to exit).

Step 4 of 5Source your .bash_profile or .bashrc

For the changes to take effect, you’ll need to source your .bash_profile or .bashrc:

source ~/.bash_profile 

or

source ~/.bashrc 

If you’re unsure which of these files you’re using, try sourcing both of them. Only one is typically used, but it won’t hurt to source both.

Step 5 of 5Verify Bash Completion

To verify that bash completion has been successfully installed and enabled, you can try typing part of a command, followed by the Tab key. If it auto-completes, then you know it’s working.

For instance, type brew followed by a space and then hit Tab twice. If you see a list of possible commands, bash completion is enabled.

And that’s it! You’ve now successfully enabled bash completion on macOS. This should greatly improve your efficiency when working in a bash environment, reducing the need to remember and type out long commands.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

  1. Hi according to > brew info bash-completion@2

    The right way to enable it is:

    Add the following line to your ~/.bash_profile:
    [[ -r “/opt/homebrew/etc/profile.d/bash_completion.sh” ]] && . “/opt/homebrew/etc/profile.d/bash_completion.sh”

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top