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.
Comments on this entry are closed.
ssh hostname completion also looks up the hosts from the ssh config file.
for host completion, one need not depend on /etc/hosts (annoying if no root!)
You can create a file with host names, one per line in the file system…personally, I use ~/.bash_hosts. Then in your profile, set (export) HOSTCOMPLETE to the path of that file. if like me, you work in several defined environ and you keep files in separate folders, combine this with direnv to automatically set HOSTCOMPLETE and the utility multiplies.
cool eh? 🙂
Great article… Thanks!!!!
Hi,
Thanks for useful material
HI,
Genious ! thanks !
I have a doubt about section 5 for hostname completion.
When you do ssh john@[TAB][TAB]
it displays the hostname associated with the local machine you are already logged in. It doesn’t cache the hostname of any remote machine you used in the past(At least thats the behavior in Mac OS X). Chances are when user is issuing ssh, it wants to reach to remote machine and not re-login back to local machine as he is already logged in, so what is the use of the [tab][tab] in ssh?
Thanks
Note that when using completion, autocomplete for entries with only one possibility occurs after only one tab, for example in a directory with the Documents and Downloads folder, typing cd Do[tab][tab] brings up the list of the two, while typing cd Doc[tab] would autocomplete.