LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-18-2015, 06:36 PM   #1
adamforum
LQ Newbie
 
Registered: Jan 2012
Posts: 5

Rep: Reputation: Disabled
How does the Linux command shell know what options are valid for a particular binary?


When I use Linux, I almost always use it through a shell -- and I'm a HUGE fan of the [tab to auto-complete] feature (e.g. start typing a file name, press tab and voila, the shell figures it out or prompts for more detail if there is still ambiguity. The other day, I noticed for the first time that the shell has some way to determine what the valid parameters for a particular binary are.

Example: I was typing the command "unzip " and then I pressed [tab] expecting the sole archive in the current folder, archive.zip to appear. To my surprise, nothing appeared... well then I realized I forgot the 'x'tract parameter to unzip...so I retried "unzip x " [tab] and the shell auto-completed to "unzip x archive.zip" as expected.

I thought this behavior may have just been specially coded into the shell for common commands like unzip, so I downloaded a third party tool -- 7z (7zip). The shell was also able to discern when I neglected to specify a required command for 7z, before executing the command.

How is this done? I assume there is some convention that must be in place (man page? --help parameter? something else??) to make this work, but I'm not sure how to Google this for the answer.

Thanks for any answers/pointers on what to search for to find the answer.

Last edited by adamforum; 07-18-2015 at 06:39 PM.
 
Old 07-18-2015, 06:45 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,981

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
I hope I'm right on this.

First off there isn't really a shell that is standard. There are many shells available, bash being maybe the most common. In bash there are different builds so that too is an issue.

Now back to your question. Example, I see no basis for your assumption that running a program would somehow decide to select options. Generally posix doesn't do that. Especially from any shell. So when you said that you completed the option portion of of the zip it now had something to do.

So, my wild guess is that your shell choice and version and build and maybe some other features allowed you to tab after this command. The tab feature was added quite later in the versions and is kind of new in way. What I am guessing is that the feature that uses the tab won't act on a blank space or maybe a single space bar because it has a form of test or other setting to tell user.

Try this. Do this same test but press the space bar after command only then tab and see what happens.
 
1 members found this post helpful.
Old 07-18-2015, 07:01 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
A number of commands install extra data in /etc/bash_completions which is probably what yoy mean, not all programs do so but a number of the common ones do, when you press tab bash looks in the conpletions folder based on what has been typed before and trys to work out what uou zwant if npt it will check in the current folder for any thing that matches what you have typed and also what you want to do for instance if you start a line with ./ bash will assume you want to run a command so will match the commamd in the current folder that are executable to what you have alreafy typed.
 
1 members found this post helpful.
Old 07-18-2015, 11:46 PM   #4
adamforum
LQ Newbie
 
Registered: Jan 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
A number of commands install extra data in /etc/bash_completions which is probably what yoy mean, not all programs do so but a number of the common ones do, when you press tab bash looks in the conpletions folder based on what has been typed before and trys to work out what uou zwant if npt it will check in the current folder for any thing that matches what you have typed and also what you want to do for instance if you start a line with ./ bash will assume you want to run a command so will match the commamd in the current folder that are executable to what you have alreafy typed.

Thanks, Keith & Jefro! Sorry for my sketchy details around shell and distro -- but you still managed to nail it. I was indeed using Bash and the /etc/bash_completions was exactly what I was looking for.

Thanks again!
AF

Last edited by adamforum; 07-19-2015 at 05:46 PM.
 
Old 07-19-2015, 06:31 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Glad to help but please dont forget to mark your threads SOLVED when you are satisfied that your question has been answered
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
Shell script - Pass command options with equal sign fabdog Linux - Newbie 5 02-13-2009 06:32 AM
Valid VGA Options? Falados Linux - Hardware 3 07-03-2005 08:43 PM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration