[SOLVED] How does the Linux command shell know what options are valid for a particular binary?
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.