LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash command Implementation (https://www.linuxquestions.org/questions/linux-newbie-8/bash-command-implementation-4175481014/)

supasoaker 10-16-2013 05:28 AM

Bash command Implementation
 
Hi all,

Bash commands have been outlined to me as follows:

Command Options Arguments

My question is, how is this implemented in the source code? Is a command a function that is called for example? Or is it done some other way?

Any examples to hand?

Thanks in advance for any help! I am simply trying to understand better the mechanics of what happens when I type a command.

druuna 10-16-2013 06:04 AM

Quote:

Originally Posted by supasoaker (Post 5046722)
Hi all,

Bash commands have been outlined to me as follows:

Command Options Arguments

My question is, how is this implemented in the source code? Is a command a function that is called for example? Or is it done some other way?

I am simply trying to understand better the mechanics of what happens when I type a command.

It isn't as simple as Command Options Arguments, the shell itself also plays a part in processing commands and its arguments.

These 2 links might shed some light:
- The Bash Parser
- (POSIX) Shell Command Line Processing Outline

Very simple example:
Code:

FOO="/tmp"
ls $FOO

In this example, when executing ls $FOO, the shell (not ls!!) first expands $FOO to /tmp. The command would now look like ls /tmp and now ls will show the content of /tmp.

supasoaker 10-16-2013 06:07 AM

thanks druuna - I didn't even know there was a bash parser :) - don't tell anyone!!! ;)

druuna 10-16-2013 06:15 AM

Quote:

Originally Posted by supasoaker (Post 5046738)
thanks druuna - I didn't even know there was a bash parser :)

This isn't bash specific. All shells use this principle in one form or another.

Having a good knowledge of these parsing rules makes CLI life much easier and explains certain behaviour.

Quote:

don't tell anyone!!! ;)
I won't :D

jpollard 10-16-2013 07:28 AM

Quote:

Originally Posted by supasoaker (Post 5046722)
Hi all,

Bash commands have been outlined to me as follows:

Command Options Arguments

My question is, how is this implemented in the source code? Is a command a function that is called for example? Or is it done some other way?

Any examples to hand?

Thanks in advance for any help! I am simply trying to understand better the mechanics of what happens when I type a command.

Part 1 is the command shell interpreter (bash/csh/tcsh/...) interacts with the parameter.

Technically, all applications receive two items of information at the main program initiation (as in the following code)
Code:

#include <stido.h> /*standard I/O library*/
#include <stdlib.h> /* some utility functions that might get used*/

/* the normal main program entry (it is possible to forces something else, but doing so is a pain,
  and non-standard */

int main(int  argc,  /* the number of parameters */
        char **argv) /* pointer to a list of pointers to the parameters */
{
    int i;

    for (i = 0; i < argc; i++) {
        printf("parameter %d = %s\n",i,argv[i]);
    }
    return(0); /* even the main is a function... 0 implies no errors were detected */
}

The above program should print all the parameters - the same function that the "echo" command does.
As illustrated, how the parameters are used is up to the program. By convention (in other words, historical), program options are identified by strings that start with a '-' character.

Some applications DON'T follow this convention - one notable one is the dd utility (data dump originally)

The dd utility interprets all the parameters as a "name=value" list, so the program itself does that.

Other programs (more conventional) use a library function "getopt" which will do most of the work, and allow the program to use a simple switch code block to interpret the options.

Still other programs will mix options and parameters - using things like -f for the option, which indicates the following parameter is a file. In the basic form this is also handled by the getopt library, any options remaining after it has finished evaluation have to handled manually (though they could just be ignored, a perfectly valid thing to do).


All times are GMT -5. The time now is 10:21 PM.