ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
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.
eg:
- 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.