Search Posts on Binpipe Blog

Autocomplete in shell scripts with COMPGEN

Today, I was required to write a shell script which will detect a directory which has a random name-ending (Eg. version-YQEG$1200 etc), enter it and run some operations inside that directory. When we work manually in commandline, this is easy because since we know that the directory begins with 'version' we type version and hit 'tab' key which autocompletes it. But how to do that in a shell script ?

That is when this command compgen came to my rescue.

So lets have a look at a command that I did not know hitherto: compgen


The compgen builtin command expands a list of arguments to generate completion matches, so it allows us to display the commands, functions, directories and aliases available for the current user. In short compgen is a command that displays commands.

So compgen is a command builtin in the bash shell so if you use this shell you have this command available in the terminal,
Let’s see how we can use it directly from the command line, as first thing let’s say that in general compgen has different options that you can use to specify which kind of thing you are looking for, so the basic usage is:

compgen [option] [word]

This generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output let’s see some examples:

Working with directories

The option for directories is -d so compgen -d will give as output the list of all the directory in the current path, or you can ask for a specific path such as:

xubuntu-home:~$ compgen -d /etc/s  /etc/sudoers.d  /etc/sysctl.d  /etc/sensors.d  /etc/skel  /etc/samba  /etc/sane.d  /etc/ssh  /etc/speech-dispatcher  /etc/systemd  /etc/sgml  /etc/ssl  /etc/sound  /etc/smartmontools  /etc/snmp  /etc/security

In this case I’ve asked compgen to show all the directory in /etc/ that start with the letter s, using just /etc/ as parameter it will show all the directories in /etc/.

Working with commands

In the same way compgen can also show all the available commands for an user with the flag -c : compgen -c if you run this command probably you’ll get a list of some thousands of commands so it’s better to use it with a | less or to show only some commands use an additional parameter that represents a part of the word you are looking for:

xubuntu-home:~$ compgen -c ss  sshd  ssgrep  ssh-vulnkey  ssindex  ssconvert  ssh-import-id  ssh  ssh-copy-id  ssh-argv0  ssh-keygen  ssh-askpass  ssh-add  ssh-keyscan  ssh-agent  ss

In this example I can see all the commands that i can use that start with ss

Other Functions

This is a short list of options that you can use with compgen:

Show all the bash built-ins commands.

compgen -b

Show all the files and directories in current path (or specify a path like in the directory)

compgen -f

Show all the bash keywords

xubuntu-home:~# compgen -k  if  then  else  elif  fi  case  esac  for  select  while  until  do  done  in  function  time  {  }  !  [[  ]]  coproc

Show all the bash functions
compgen -A function

Show all the alias available for the current user

xubuntu-home:~$ compgen -a  vps  egrep  fgrep  grep  ls

Show all the usernames defined in the server:

xubuntu-home:~# compgen -u s  sys  sync  syslog  speech-dispatcher  saned  sshd  sslh

Show all the groups defined in the server:

xubuntu-home:~# compgen -g p  proxy  plugdev  pulse  pulse-access  postfix  postdrop

35 comments:

  1. Excellent web site you've got here.. It’s hard to find good quality writing like yours these days.
    I honestly appreciate people like you! Take care!!

    ReplyDelete
  2. Very good info. Lucky me I found your website by chance (stumbleupon).
    I've book-marked it for later!

    ReplyDelete
  3. Great site you have here.. It’s difficult to find high quality writing
    like yours these days. I really appreciate individuals like you!
    Take care!!

    ReplyDelete
  4. I'd like to thank you for the efforts you have
    put in penning this site. I'm hoping to see the same high-grade content by you
    in the future as well. In truth, your creative
    writing abilities has motivated me to get my own, personal site now ;)

    ReplyDelete
  5. This is the perfect webpage for everyone who wishes to find out about this topic.

    You realize so much its almost hard to argue with you
    (not that I really will need to…HaHa). You definitely
    put a fresh spin on a topic that has been written about for ages.

    Excellent stuff, just excellent!

    ReplyDelete
  6. Oh my goodness! Incredible article dude! Thank you so much, However I am encountering issues with your
    RSS. I don’t understand why I can't subscribe
    to it. Is there anybody else getting identical RSS issues?
    Anyone who knows the answer will you kindly respond? Thanks!!

    ReplyDelete
  7. I like reading through an article that will make men and women think.
    Also, thank you for allowing for me to comment!

    ReplyDelete
  8. Good article. I certainly appreciate this website. Stick with it!

    ReplyDelete
  9. Greetings! Very helpful advice within this article!
    It is the little changes that will make the most important
    changes. Thanks a lot for sharing!

    ReplyDelete
  10. I have to thank you for the efforts you have put in writing this blog.
    I really hope to check out the same high-grade blog posts from you in the future
    as well. In fact, your creative writing abilities
    has encouraged me to get my own website now ;)

    ReplyDelete
  11. I really love your blog.. Excellent colors & theme.
    Did you build this web site yourself? Please reply back as I’m looking to
    create my very own site and would love to
    learn where you got this from or exactly what the theme is called.

    Thank you!

    ReplyDelete
  12. I was able to find good advice from your blog posts.

    ReplyDelete
  13. I seriously love your site.. Pleasant colors & theme.
    Did you develop this site yourself? Please reply back as I’m looking to create my own personal
    blog and would like to find out where you got this
    from or what the theme is called. Thanks!

    ReplyDelete
  14. I love looking through an article that can make men and
    women think. Also, many thanks for permitting me to comment!

    ReplyDelete
  15. I really like reading through a post that will make people
    think. Also, thanks for permitting me to comment!

    ReplyDelete
  16. Very nice blog post. I definitely appreciate this site.
    Keep it up!

    ReplyDelete
  17. Spot on with this write-up, I honestly believe this web site needs a lot more attention. I’ll
    probably be back again to read more, thanks for the info!

    ReplyDelete
  18. You are so interesting! I don't believe I've read anything
    like this before. So nice to find someone with original thoughts on this subject.
    Seriously.. many thanks for starting this up. This website is something that is required on the internet,
    someone with a little originality!

    ReplyDelete
  19. This is a great tip especially to those fresh to the blogosphere.
    Short but very accurate info… Thank you for sharing this one.
    A must read article!

    ReplyDelete
  20. May I simply say what a comfort to find somebody who genuinely knows what they are talking
    about online. You definitely understand how to bring an issue to light and make it
    important. More people should look at this and understand this side of
    your story. I was surprised you're not more popular since
    you definitely possess the gift.

    ReplyDelete
  21. Your style is really unique compared to other folks I've read stuff from.
    Thanks for posting when you have the opportunity, Guess I'll just bookmark this site.

    ReplyDelete
  22. Very good post. I am going through many of these issues
    as well..

    ReplyDelete
  23. bookmarked!!, I like your website!

    ReplyDelete
  24. Hi, I do believe this is an excellent site. I stumbledupon it ;) I'm going to return yet again since I
    book-marked it. Money and freedom is the best way to change,
    may you be rich and continue to guide others.

    ReplyDelete
  25. This is the right website for anyone who hopes to understand this
    topic. You realize so much its almost hard to argue with you (not that I personally would want to…HaHa).
    You definitely put a fresh spin on a topic that's been discussed for decades.
    Wonderful stuff, just wonderful!

    ReplyDelete
  26. There's definately a lot to learn about this topic. I like all the points you made.

    ReplyDelete
  27. Excellent post. I absolutely appreciate this site.
    Thanks!

    ReplyDelete
  28. There is certainly a lot to find out about this subject.
    I like all of the points you've made.

    ReplyDelete
  29. Good article. I will be going through a few of these issues as well..

    ReplyDelete
  30. It’s nearly impossible to find educated people for this topic,
    but you seem like you know what you’re talking about! Thanks

    ReplyDelete
  31. Wonderful article! We are linking to this great content on our website.
    Keep up the great writing.

    ReplyDelete
  32. This page truly has all of the info I wanted about this subject and didn’t know who to ask.

    ReplyDelete
  33. Aw, this was an incredibly good post. Taking the time
    and actual effort to produce a good article… but what can I say… I
    hesitate a lot and don't seem to get anything done.

    ReplyDelete
  34. Oh my goodness! Incredible article dude! Thank you so much, However I am encountering issues with your RSS.
    I don’t understand the reason why I am unable
    to join it. Is there anybody having the same RSS problems?
    Anyone that knows the solution will you kindly respond?
    Thanx!!

    ReplyDelete
  35. Hello! I simply want to offer you a huge thumbs up for the great info you have here on this post.

    I am returning to your site for more soon.

    ReplyDelete

Hi, Leave a comment here and one of the binary piper's will reply soon :)