Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to ask bash how it would complete a string as if I had typed it in a shell and hit tab. For example, if I type

ls /[TAB][TAB]

I see the list of files and dirs in / that could possibly complete the ls command. How do I ask bash how it would complete 'ls /' without typing it and hitting tab? I want something like:

query_complete 'partial command line string'

I read the man page for complete and compgen, but couldn't figure out how to do it with them.

Note: 'ls /' is not the actual command I'm interested in, just an example. I am looking for a general solution for any arbitrary string representing a partial command line.

share|improve this question

You could try:

compgen -o default /
share|improve this answer
thanks, maybe I didn't make it clear enough; the 'ls /' was just an example. Other commands have much more complex autocomplete scripts defined. I really want something that can take any arbitrary string and output the completion hints as if you had typed that string. – Ryan McKay Feb 4 '11 at 23:09
@Ryan: Bash doesn't have that kind of reflexive functionality. I'm assuming you want all the programmable completions performed by the bash-completion package, too. Have you looked at the contents of that package? What you want to do falls somewhere between extremely complex and not possible. – Dennis Williamson Feb 5 '11 at 1:01

Apparently the Awesome WM has a neat little trick getting hold of this.

But I can't seem to get it working properly. Let me know if you have better luck.

share|improve this answer

You could use a function that uses bash's Parameter Expansion, like this:

expand(){ echo ${a:+$1}; }; expand "ls /*"

But you'll need to replace your [TAB][TAB] with "*".

share|improve this answer

I think a good option for you is to look into bash functions you can write for your .bashrc file. My guess is that you end up typing commands over and over again, and you want to speed things up. Here's an example from my .bashrc file:

newmod() {
mkdir -p ${1}/{manifests,files,templates} && \
echo "class $1" >> ${1}/manifests/init.pp && \
echo "class ${1}::conf" >> ${1}/manifests/conf.pp && \
echo "class ${1}::packages" >> ${1}/manifests/packages.pp

I create lots of puppet modules. This function, when invoked from the shell, does the following work for me:

[configmgr@mback-dev ~]$ newmod testmodule
[configmgr@mback-dev ~]$ tree testmodule/
|-- files
|-- manifests
|   |-- conf.pp
|   |-- init.pp
|   `-- packages.pp
`-- templates

3 directories, 3 files
[configmgr@mback-dev ~]$ 

The files get populated with scaffolding for the modules class, packages, and configuration. This saves me tons of typing!

HTH, ztron

share|improve this answer
How does this relate to getting bash string completion results programmatically? – Bill Weiss Jun 8 '11 at 19:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.