LinuxQuestions.org
Help answer threads with 0 replies.
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 05-11-2020, 12:41 PM   #1
Victor43
Member
 
Registered: May 2020
Posts: 46

Rep: Reputation: Disabled
How does bash process user input from the command line


Running Ubuntu 19.10 have a question about Bash

How does bash process user input such as a command like "ls -a" ? How is this actually done in detail as possible ?
 
Old 05-11-2020, 12:49 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
From here:
Quote:
After a command is entered, the following things are done:
  1. Command is entered and if length is non-null, keep it in history.
  2. Parsing : Parsing is the breaking up of commands into individual words and strings
  3. Checking for special characters like pipes, etc is done
  4. Checking if built-in commands are asked for.
  5. If pipes are present, handling pipes.
  6. Executing system commands and libraries by forking a child and calling execvp.
  7. Printing current directory name and asking for next input.
Here is another explanation.

And here are all the gory details. In particular:
Quote:
...
Now, thought experiment: What happens when you type ls into a shell? You know the fork(), exec() and wait() cycle that occurs, along with tty. But, even before this happens, ls is just another utility function, right? Which means there’s a program file somewhere which has the C code that does fork() and everything else.
...
and so on.

Last edited by shruggy; 05-11-2020 at 01:08 PM.
 
1 members found this post helpful.
Old 05-11-2020, 01:47 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
How does bash process user input such as a command like "ls -a"
http://ftp.gnu.org/pub/gnu/coreutils/
https://github.com/coreutils/coreuti...aster/src/ls.c
http://www.gnu.org/software/coreutils/
http://git.savannah.gnu.org/cgit/cor.../tree/src/ls.c
 
Old 05-11-2020, 04:22 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
In short:
it forks a subshell that execs into an "ls -a".
 
Old 05-12-2020, 04:30 PM   #5
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks for the response. Can you tell me whether the above links are for the source code for bash ?
 
Old 05-13-2020, 02:02 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
No, they are for the source code of ls.
 
Old 05-13-2020, 02:51 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by Victor43 View Post
Can you tell me whether the above links are for the source code for bash ?
You can use the package manager to get the actual source, with modifications, used by your distro.
If you are on Ubuntu or other dpkg-based distro, you can fetch the source package with apt-src or apt-get like this:

Code:
cd /tmp/
apt-get source bash
ls -ld bash*
cd bash-5.0
You will find all the C files there that make up Bash.
 
Old 05-13-2020, 03:01 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by Victor43 View Post
How does bash process user input such as a command like "ls -a" ? How is this actually done in detail as possible ?
That is written on the man page of bash. Look for command execution. I cannot explain better (do not need to read the source code)
 
1 members found this post helpful.
Old 05-13-2020, 10:43 AM   #9
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
From here:


Here is another explanation.

And here are all the gory details. In particular:

and so on.
Thank you shruggy. I have a question to ask. How did you figure out what system calls are being executed ? Did you use ltrace and strace or even gdb ?
 
Old 05-13-2020, 10:46 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
yes, strace will tell you.
But in general those are the fork and exec (to start another process).
 
Old 05-31-2020, 02:14 PM   #11
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
yes, strace will tell you.
But in general those are the fork and exec (to start another process).
Many thanks pan64.
 
Old 06-03-2020, 01:53 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
Actually on a recent Linux system strace will report clone() rather than fork().
 
Old 07-21-2020, 02:57 PM   #13
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
yes, strace will tell you.
But in general those are the fork and exec (to start another process).
Thanks again but I could not find the system call fork() (when running strace ls -l) maybe I've missed something ? Also when I execute strace ls -l within a terminal window I see execve at the top of the list not exec.

Please advise. Thanks.
 
Old 07-22-2020, 01:29 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
what to advice? there was no fork that's why you can't find it.
ls is a simple binary, it does not fork any other process.
about exec: see the man page: https://man7.org/linux/man-pages/man3/exec.3.html (and the description)
 
Old 07-08-2021, 10:08 AM   #15
Victor43
Member
 
Registered: May 2020
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
In short:
it forks a subshell that execs into an "ls -a".
Thanks for the response but what does the above mean in layman's terms "forks a subshell" ? Please correct me if I'm wrong but a fork() system call is called which in turn creates a child process that in turn calls system function call execve() which processes the "ls -a" command. Does this sound correct ?

Thanks

Last edited by Victor43; 07-19-2021 at 12:29 PM.
 
  


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
Bash Script For Reading User Input then Compressing That Input to Tar file braveranger Linux - Newbie 13 11-15-2017 09:36 AM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 08:04 AM
[SOLVED] Awk: Input from one line, execute program; input from next line, execute program C.L. Programming 9 09-27-2010 12:06 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM

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

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