In Linux, by default Bash provides the following standard completion for users to use in the command line:
- Variablename completion
- Username completion
- Executable completion
- Filename and directory completion
- Hostname completion
1. Variablename Completion
After typing $ in terminal, pressing tab twice will display all available shell variables as shown below.
$ echo $[TAB][TAB] $_ $COMP_POINT $HOSTTYPE $PS1 $_backup_glob $COMPREPLY $IFS $PS2 $BASH $COMP_TYPE $inx $PS4 $BASH_ALIASES $COMP_WORDBREAKS $LANG $PWD $BASH_ARGC $COMP_WORDS $LANGUAGE $RANDOM $BASH_ARGV $cur $LESSCLOSE $redir $BASH_CMDS $cword $LESSOPEN $SECONDS $BASH_COMMAND $DIRSTACK $LINENO $SHELL $BASH_COMPLETION_COMPAT_DIR $DISPLAY $LINES $SHELLOPTS $BASH_LINENO $errx $LOGNAME $SHLVL $BASHOPTS $EUID $LS_COLORS $split $BASHPID $exclude $MACHTYPE $SUDO_COMMAND $BASH_REMATCH $flag $MAIL $SUDO_GID $BASH_SOURCE $FUNCNAME $MAILCHECK $SUDO_UID $BASH_SUBSHELL $GROUPS $OLDPWD $SUDO_USER $BASH_VERSINFO $__grub_script_check_program $OPTERR $suffix $BASH_VERSION $HISTCMD $OPTIND $TERM $__colormgr_commandlist $HISTCONTROL $OSTYPE $UID $COLORTERM $HISTFILE $outx $USER $COLUMNS $HISTFILESIZE $PATH $USERNAME $COMP_CWORD $HISTSIZE $PIPESTATUS $words $COMP_KEY $HOME $PPID $XAUTHORITY $COMP_LINE $HOSTNAME $prev $_xspecs
2. Username Completion
When you press tab twice, after tilde (~), bash will automatically start the username completion.
$ cd ~[TAB][TAB] ~bala/ ~raj/ ~jason/ ~randy/ ~john/ ~ritu/ ~mayla/ ~thomas/ ~nisha/ ~www-data
Please note that this doesn’t pick-up the username from the home directory. Instead, it displays all the available username from /etc/passwd file
3. Pathname Completion for Executables
When you are trying to execute a command, if the executable has execute permission, it will automatically get completed, if a single match is found as shown in the example below.
$ ls -l /etc/init.d/reboot -rwxr-xr-x 1 root root 639 Jan 30 2013 /etc/init.d/reboot $ /etc/init.d/reb[TAB][TAB] $ /etc/init.d/reboot
When multiple matches are found, it will display available commands.
4. Filename and Directory Completion
This completion is for filename and directory names that are occurring at second and subsequent position on the command line. Unlike the above example, this doesn’t check for any permissions, and will just display all the available files and directories.
$ ls countfiles.sh dir1 dir2 dir3 $ cat [TAB][TAB] countfiles.sh dir1 dir2 dir3 $ cat c[TAB][TAB] $ cat countfiles.sh
Also, when there are lot of files to be displayed, instead of displaying all of the possibilities on the screen, which might get very confusing, it will give the following warning message.
$ ls -l /etc/ Display all 228 possibilities? (y or n)
5. Hostname Completion
In order to get the hostnames to connect to, press tab twice after @ symbol as shown below:
$ ssh john@[TAB][TAB] @dev-db @fileserver @qa-server @prod-db @localhost @web-server
You can use this hostname completion feature alogn with any command where you can give @ for hostname. For example, you can use this with scp also as shown below:
$ scp filename.txt john@[TAB][TAB] @dev-db @fileserver @qa-server @prod-db @localhost @web-server
Please note that this picks-up the available hostnames from /etc/hosts file.