Posted on
Advanced

Bash completions: Writing and customizing

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Bash Completions: A Guide to Writing and Customizing Your Own

Command-line interfaces can be intimidating for newcomers and inconvenient for the experienced due to the need to remember numerous commands and their associated options. Bash, the default shell on many Linux distributions, includes a feature known as "Bash completion" which helps users manage the complexity by providing automatic suggestions and completions when users type commands.

What is Bash Completion?

Bash completion is a functionality through which the Bash shell automatically suggests or completes file names, command names, and other arguments when a user types a command and presses the Tab key. This feature significantly speeds up the typing of commands by reducing the amount of text the user needs to type and helps avoid typographical errors.

Installing Bash Completion

Bash completion might not be installed by default, depending on your Linux distribution. Here’s how to install it using different package managers:

  • Debian/Ubuntu: Use apt

    sudo apt install bash-completion
    
  • Fedora: Use dnf

    sudo dnf install bash-completion
    
  • openSUSE: Use zypper

    sudo zypper install bash-completion
    

After installation, you might need to log out and back in, or reload your bash configuration with source ~/.bashrc to take effect.

Enabling and Using Bash Completion

Once installed, bash completion is typically enabled by default. You can check if bash completion is working by typing a partial command and then pressing the Tab key. For example, type sudo apt-get up and press Tab; if bash completion is working, it should complete the command to sudo apt-get update.

Writing Custom Bash Completion Scripts

Power users can write their custom bash completion scripts to add support for commands that do not have completion scripts by default. Here's a brief guide on how to create a completion script:

  1. Identify the Command: Determine the command for which you want to add auto-completion.

  2. Create a Completion Script: Bash completion scripts are typically stored in /etc/bash_completion.d/. Here's an example for a hypothetical command named mycommand:

    #!/bin/bash
    
    _mycommand_completions()
    {
       COMPREPLY=($(compgen -W "option1 option2 option3" -- ${COMP_WORDS[COMP_CWORD]}))
    }
    
    complete -F _mycommand_completions mycommand
    

    In this script, compgen generates completion responses suitable for the current context (given the word being completed). COMP_WORDS is an array of all words in the current command line. COMP_CWORD is an index into ${COMP_WORDS}, representing the word to complete.

  3. Install the Script: After writing the script, you need to make the file executable and move it to /etc/bash_completion.d/.

    chmod +x mycommand_completion
    sudo mv mycommand_completion /etc/bash_completion.d/
    
  4. Reload Bash: Use the source command to reload the bash configuration or restart your terminal for the changes to take effect.

    source ~/.bashrc
    

Testing and Debugging

After setting up your completion script, you should test it by trying to complete commands that use your new script. If the completion does not work as expected, you can debug your script by adding set -x at the top of your script to enable debug mode, which prints commands and their arguments as they are executed.

Conclusion

Writing custom bash completion scripts is a powerful way to enhance productivity, ensuring faster and error-free command line usage. For those who frequently use the terminal, investing time to set up and customise bash completion is certainly worthwhile. Whether you're using Debian, Fedora, or openSUSE, the basic principles of bash completion remain the same, streamlining your command-line workflow efficiently.