Bash can auto complete your filenames and command name. It can also auto complete lots of other stuff such as:
=> Usernames
=> Hostname
=> Variable names
=> Fine tuning files and names with ESC keys
Match variable
If the text begins with $, bash will look for a variable. For example, open terminal and type echo $T and press TAB key, shell will complete that to echo $TERM:
echo $T {hit-tab-key}
Match Username
If the test begins with ~ (tilde), bash will look for a user name. For example, open terminal and type cat ~g and press TAB key, shell will complete that to cat ~guess/file.txt:
cat ~g {hit-tab-key}
Match hostname
If the test begins with @, bash will look for a host name. For example, open terminal and type scp file.txt tom@o and press TAB key, shell will complete that to scp file.txt tom@oldbox:
scp file.txt tom@o {hit-tab-key}
Please note that you need proper host resoultion configured to work with this hack via NIS or hosts file. Also after shell completes the command name / username or filename hit the [ENTER] key.
Fine tunning Shell Completing stuff with ESC key
Bash allows you to fine tune file completion using ESC key combinations. People get amazed when I use ESC combination in front of them. For example, to inserts all possible completions into your command use ESC+*. Let us see how to backup all /etc/*.conf files, type the command:
tar -zcvf /dev/rt0 /etc/*.conf {hit ESC followed by *}
As soon as you hit Esc+*, shell replaces the /etc/*.conf part with names of all matching wild card patterns
tar -zcvf /dev/rt0 /etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/debconf.conf ....
To displays all possible completions of command or filenames or username type ESC+?, to display all username start with the word le, type
cat ~le {hit ESC followed by ?}
complete command
There is also in built command called complete. It is used to specify how arguments are to be completed for a command. For example, when you type passwd (or any other user admin command such as su / usermod etc) and hit tab key, bash will show you a list of all available users i.e. all user admin related commands will see only user names:
complete -u su usermod userdel passwd chage write chfn groups slay w
Now type passwd and hit tab key to see all username:
passwd {hit tab key}
Output:
avahi bin dhcp gdm haldaemon klog mail news root sys uucp avahi-autoipd cupsys dnsmasq gnats hplip list man nobody sshd syslog vivek backup daemon games guest irc lp messagebus proxy sync telnetd www-data vivek@vivek-desktop:/tmp$ passwd
Cool, huh? There is a nice file included with almost all distro to complete lots of stuff using complete command. Just add following line to your bash startup file:
source /etc/bash_completion
Further reading:
- Bash man page and complete command section
- /etc/bash_completion file
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
@yoooo,
/home/you/.bashrc or /home/you/.bash_profile
so where do we put: source /etc/bash_completion ?
cat ~le {hit ESC followed by ?}
hit tab key twice work same.
Scott,
It should work on Ubuntu and yes SHELLOPTS is set to emacs for me. I’m using bash 3.x
Got it. You must have “set -o vi” by default. Ubuntu is “set -o emacs” by default.
The example:
tar -zcvf /dev/rt0 /etc/*.conf {hit ESC followed by *}
Didn’t work on Ubuntu but did on FC4. Any ideas why?