5 Standard Completion Available in Linux Bash Command Line

by Balakrishnan Mariyappan on November 11, 2013

In Linux, by default Bash provides the following standard completion for users to use in the command line:

  1. Variablename completion
  2. Username completion
  3. Executable completion
  4. Filename and directory completion
  5. 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.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 7 comments… read them below or add one }

1 Bernhard November 12, 2013 at 3:33 am

ssh hostname completion also looks up the hosts from the ssh config file.

2 Derick Qua November 12, 2013 at 4:10 am

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? :)

3 Bob November 12, 2013 at 8:56 am

Great article… Thanks!!!!

4 Jalal Hajigholamali November 12, 2013 at 9:31 am

Thanks for useful material

5 Mustapha Oldache November 12, 2013 at 12:33 pm

Genious ! thanks !

6 Shakil November 12, 2013 at 6:14 pm

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?


7 Trevor December 2, 2013 at 10:58 am

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.

Leave a Comment

Previous post:

Next post: