• 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
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

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • OpenShift OctoPress Auto install Script

    OpenShift OctoPress Auto install Script is an Advanced Script to Run OctoPress on Free OpenShift PaaS Practically Without Any Knowing Ruby or Git.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

  • Adding Help & Option in Bash Script

    Many New Hobbyist Programmers and Students Write Bash Scripts. Adding Help & Option in Bash Script is Easy. Here is Basic Guide With Example.

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

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
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