LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-16-2013, 05:28 AM   #1
supasoaker
Member
 
Registered: Nov 2008
Posts: 47

Rep: Reputation: 16
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.
 
Old 10-16-2013, 06:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by supasoaker View Post
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.
 
Old 10-16-2013, 06:07 AM   #3
supasoaker
Member
 
Registered: Nov 2008
Posts: 47

Original Poster
Rep: Reputation: 16
thanks druuna - I didn't even know there was a bash parser - don't tell anyone!!!
 
Old 10-16-2013, 06:15 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by supasoaker View Post
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
 
Old 10-16-2013, 07:28 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by supasoaker View Post
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).
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wc command implementation mahdi.m Programming 9 02-17-2013 01:11 PM
tee command implementation hellorajni Linux - Software 2 11-10-2010 12:54 PM
Implementation of LS command imrrann Linux - General 1 09-04-2010 05:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 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