Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-25-2005, 04:53 AM   #1
LQ Newbie
Registered: Nov 2002
Location: Republic Of China
Posts: 9

Rep: Reputation: 0
How The Unix OS translate the shell command

for example: ls -a

how the unix read "ls" and "-a"

My firend think unix read the command with regex

I don't think so.

Last edited by flavor; 10-25-2005 at 04:54 AM.
Old 10-25-2005, 05:53 AM   #2
Registered: May 2003
Posts: 162

Rep: Reputation: 28
http://<br />

Yes the shell takes the parameters as regular expressions if they are specified in correct format.

For example

int main(int argc,char*argv[])
        int i;
after compiling this program if you run

$ a.out *
the output will show the files in the current directory. This is because shell interprets *
as an operator which means

"Matches zero or more of the preceding characters or expressions. Example: ho*p matches hp, hop and hoop."
Old 10-25-2005, 09:58 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,471

Rep: Reputation: 221Reputation: 221Reputation: 221
depends which shell I should imagine.

Bash i think reads with readline
Old 10-25-2005, 12:51 PM   #4
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
bigearsbilly: I think OP meant how it's parsed, not read.

I don't know, since I haven't hacked on bash, but you could always try #bash on freenode (assuming you use bash). It is, of course, an implementation detail of the individual shell.

My best guess would be that they use flex and yacc, but I don't know.

hth --Jonas
Old 10-25-2005, 01:02 PM   #5
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
A shell is simple enough in theory, but remember over time they got the simple stuff working and added more complex features (like shell expansion).

Basically a shell runs in a loop, writing a prompt to stdout, reading a line of any length to stdin, breaking apart the command, then forks + exec's the correct process.


- step 1 (print prompt)
[user@bash ~]$

- step 2 (read a line until <ENTER> is pressed - '\n')
[user@bash ~]$ ls -l /usr/lib

- step 3 (shell determines if this is a 'built-in' or external command)
- step 4 (realizes 'ls' is an external program)
- step 5 (forks new process, exec's ls and passes all arguments to ls)
- step 6 (ls runs, processes its own arguments, prints output to stdout)
[user@bash ~]$ ls -l /usr/lib
output of all files in /usr/lib

- step 7 (bash takes over control when ls finishes executing, prints prompt)
[user@bash ~]$

- step 8 (bash waits for next command to be entered until '\n' is pressed)

Its more complex than that, but thats the jist of it. Remember bash is filled with features, so it has to determine aliases, execute programs inline, set up pipes (I think), perform wildcard expansion and more. There is an order to this, if you're really interested I learned alot of this from a shell programming book. I forget most of it because it was for school and I was never as interested in bash as I was in other languages, but a good shell book or tutorial might help you understand the process.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
UNIX shell programming Tozilla Programming 39 02-26-2009 08:29 AM
unix shell script cxy0481 Programming 9 11-20-2005 09:15 AM
Can anybody really help me to create my own shell for linux or unix. sri_hassan Programming 2 10-10-2005 08:57 AM
Unix Shell Programming If Statement ']['HeBroken Programming 2 12-06-2004 04:21 PM
A Unix shell question a2715mt Linux - General 2 08-25-2004 04:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 PM.

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