• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Bash Completion Tutorial

By Abhishek Ghosh November 4, 2014 9:47 am Updated on November 4, 2014

Bash Completion Tutorial

Advertisement

Here is a Bash Completion Tutorial so that a command line user can either use the existing Bash completion examples or create own Bash Script. Bash Completion Tutorial, to certain extent is a misnomer. We have discussed about Terminal and Shell. Better terminology will be Command line completion, which is a common feature of all the command line interpreters. The program actually automatically fills the partially typed commands after proper setup. There are ready made scripts as well as custom scripts can be used. Again the need of knowing to write shell scripts becomes apparent.

 

Bash Completion Tutorial : Basics First

 

Bash uses GNU Readline Library which features history and basic completion. One can run this command from command line (for any UNIX, OS X/Mac, Linux, Unix-like system) to know about it :

Bash Completion Tutorial
Vim
1
curl http://cnswww.cns.cwru.edu/php/chet/readline/README > readline.txt && nano readline.txt

The Debian bash package has a command line completion file located at /etc/bash_completion which sets up some common support. We just need to execute it by running . /etc/bash_completion command. If you type /ro and press TAB key, it becomes /root. You can create an Alioth account at https://alioth.debian.org for own pull. You know about unix pipeline. If you run this command :

Advertisement

---

Vim
1
bind -p | grep complete

shows to which key the special feature is bound on the particular system. Bash Completion on deb system is a daemon, you can check /etc/bash_completion.d. The reason to mention it is to possibly stopping the daemon before logging out from SSH session for deb based server. Helping a script kiddie, somehow logged in with additional auto-complete, is not good idea!

Keep in mind – Ubuntu or other deb based system, might not have the script by default.
The Readline library, manages the command line editing, and calls back to bash when tab is pressed, to enable completion. Bash itself has a feature named complete to define a completion mechanism for individual commands. With complete «options» «command» we can define the completion for some command, which is written here :

Vim
1
http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?%28bash.info.gz%29Programmable%2520Completion

Bash Completion Tutorial

 

Bash Completion Tutorial : Homebrew + OSX + iTerm2 Setup

 

On Mac, Homebrew + OSX + iTerm2 Setup is commonly used. Oh-My-ZSH means ZSH, means Z Shell. You possibly have the plugin :

Vim
1
https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/brew/_brew

In other words, test it first with typing some commands and hitting Tab. When you are doing SSH to Server from iTerm2, thats not your local computer’s iTerm2. We get the echo of the output, but machine is the remote server. SSH is different topic, you need to install the bash-completion from brew install bash-completion and this will be the output :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
==> Patching
patching file bash_completion
==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3
==> make install
==> Caveats
Add the following lines to your ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
 
Homebrew's own bash completion script has been installed to
  /usr/local/etc/bash_completion.d
 
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

The bug is, the lines should be :

Vim
1
2
3
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion.d
fi

They forgotten the .d part. You can load the environment by running source ~/.bash_profile, it should not throw error.

Possibly most ask for Shell Integration :

Vim
1
http://iterm2.com/shell_integration.html#/section/home

Mike has an excellent article on yummy and useful Terminal :

Vim
1
http://mikebuss.com/2014/02/02/a-beautiful-productive-terminal-experience/

You can easily waste a day reading his great writings. Unfortunately, none reads the good stuffs or probably Google is not at the level of understanding the good stuffs. The screenshot is from Mike Buss’s post.

 

Bash Completion Tutorial : Autocomplete in apt-get

 

In latest Ubuntu, the bash-complete package is omitted. Script hichhandles completions for service is in /usr/share/bash-completion/bash_completion and it looks for service names in /etc/rc.d/init.d and /etc/init.d, and output is systemctl list-units --full --all. You should know about run level. You have to run this command – sudoedit /usr/share/bash-completion/completions/apt and paste this :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Debian apt(8) completion                             -*- shell-script -*-
 
_apt()
{
    local cur prev words cword
    _init_completion || return
 
    local special i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${words[i]} == @(list|search|show|update|install|remove|upgrade|full-upgrade|edit-sources|dist-upgrade|purge) ]]; then
            special=${words[i]}
        fi
    done
 
    if [[ -n $special ]]; then
        case $special in
            remove|purge)
                if [[ -f /etc/debian_version ]]; then
                    # Debian system
                    COMPREPLY=( $( \
                        _xfunc dpkg _comp_dpkg_installed_packages $cur ) )
                else
                    # assume RPM based
                    _xfunc rpm _rpm_installed_packages
                fi
                return 0
                ;;
            *)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) )
                return 0
                ;;
        esac
    fi
 
    case $prev in
        -c|--config-file)
             _filedir
             return 0
             ;;
        -t|--target-release|--default-release)
             COMPREPLY=( $( apt-cache policy | \
                 command grep "release.o=Debian,a=$cur" | \
                 sed -e "s/.*a=\(\w*\).*/\1/" | uniq 2> /dev/null) )
             return 0
             ;;
    esac
 
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-d -f -h -v -m -q -s -y -u -t -b -c -o
            --download-only --fix-broken --help --version --ignore-missing
            --fix-missing --no-download --quiet --simulate --just-print
            --dry-run --recon --no-act --yes --assume-yes --show-upgraded
            --only-source --compile --build --ignore-hold --target-release
            --no-upgrade --force-yes --print-uris --purge --reinstall
            --list-cleanup --default-release --trivial-only --no-remove
            --diff-only --no-install-recommends --tar-only --config-file
            --option --auto-remove' -- "$cur" ) )
    else
        COMPREPLY=( $( compgen -W 'list search show update install
            remove upgrade full-upgrade edit-sources dist-upgrade
            purge' -- "$cur" ) )
    fi
 
    return 0
} &&
complete -F _apt apt
 
# ex: ts=4 sw=4 et filetype=sh

As you can see, this script has some pre-defined words :

Vim
1
if [[ ${words[i]} == @(list|search|show|update|install|remove|upgrade|full-upgrade|edit-sources|dist-upgrade|purge) ]]

You can change the things. Reload the environment by running source ~/.bashrc.

 

Bash Completion Tutorial : Own Scripts

 

The scripts inside /etc/bash_completion.d/ does the work for Debian. There are hundreds of scripts available for various advanced usage. Well, you can try to do a homework on which, on your Mac iTerm2, on typing ssh root followed by hitting Tab will return only few results which actually you SSH to. Hint is, the SSH complete function loads from here :

Vim
1
2
3
4
cd /usr/share/zsh/ && ls
cd <version-number> && ls
cd functions
nano _ssh

If you do not edit it, it will load a huge list, just the output :

Vim
1
2
3
ssh root@
# upon hitting [Tab]
zsh: do you wish to see all 289 possibilities (289 lines)? [y/n]

It is echoing all the known hosts and hundreds of stuffs. It also shows ZSH’s corrective capability – 289 is a big list, ZSH gave the option to think. Ok, another clue – this should be the format of the syntax needed to be added in some profile file :

Vim
1
2
3
4
5
SSH_COMPLETE=( $(cut -f1 -d' ' ~/.ssh/your_hosts |\
                 tr ',' '\n' |\
                 sort -u |\
                 grep -e '[:alpha:]') )
complete -o default -W "${SSH_COMPLETE[*]}" ssh

~/.ssh/your_hosts will contain the list of your needed to SSH servers. Last clue – edit .zshrc.

Tagged With bash configure completion tutorial , brew zsh completion loc:US

This Article Has Been Shared 401 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Bash Completion Tutorial

  • Cron and Cron Job : Do Works Without Shell

    Cron is less known but almost universally present feature on any kind of server with an usual control panel.One can perform works via Cron without Shell Access.

  • ReactOS : Windows Application Compatible Open Source OS

    ReactOS is a free operating system that is compatible with Microsoft Windows applications with the goal to complete binary compatibility for Microsoft Windows.

  • Desktop Virtualization with Oracle VirtualBox

    Desktop Virtualization provides a complete work environment. Custom configured operating system of the guest systems are stored on the host systems.

  • Create User Named Github Page : Easy Guide

    Create User Named Github Page just from your browser. For a basic webpage for your own named Github page, practically few steps are required to create yours.

  • UNIX Commands Video Guide to Get Started

    UNIX Commands Video Guide is for the starters to push towards the black screen to understand how basically we need to work with command line tool or CLI.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Zebronics Pixaplay 16 : Entry Level Movie Projector Review February 2, 2023
  • What is Voice User Interface (VUI) January 31, 2023
  • Proxy Server: Design Pattern in Programming January 30, 2023
  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Bash Completion Tutorial," in The Customize Windows, November 4, 2014, February 3, 2023, https://thecustomizewindows.com/2014/11/bash-completion-tutorial/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT