LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How does bash process user input from the command line (https://www.linuxquestions.org/questions/linux-newbie-8/how-does-bash-process-user-input-from-the-command-line-4175675020/)

Victor43 05-11-2020 12:41 PM

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 ?

shruggy 05-11-2020 12:49 PM

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.

teckk 05-11-2020 01:47 PM

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

MadeInGermany 05-11-2020 04:22 PM

In short:
it forks a subshell that execs into an "ls -a".

Victor43 05-12-2020 04:30 PM

Thanks for the response. Can you tell me whether the above links are for the source code for bash ?

shruggy 05-13-2020 02:02 AM

No, they are for the source code of ls.

Turbocapitalist 05-13-2020 02:51 AM

Quote:

Originally Posted by Victor43 (Post 6122286)
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.

pan64 05-13-2020 03:01 AM

Quote:

Originally Posted by Victor43 (Post 6121826)
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)

Victor43 05-13-2020 10:43 AM

Quote:

Originally Posted by shruggy (Post 6121832)
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 ?

pan64 05-13-2020 10:46 AM

yes, strace will tell you.
But in general those are the fork and exec (to start another process).

Victor43 05-31-2020 02:14 PM

Quote:

Originally Posted by pan64 (Post 6122512)
yes, strace will tell you.
But in general those are the fork and exec (to start another process).

Many thanks pan64.

MadeInGermany 06-03-2020 01:53 AM

Actually on a recent Linux system strace will report clone() rather than fork().

Victor43 07-21-2020 02:57 PM

Quote:

Originally Posted by pan64 (Post 6122512)
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.

pan64 07-22-2020 01:29 AM

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)

Victor43 07-08-2021 10:08 AM

Quote:

Originally Posted by MadeInGermany (Post 6121910)
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


All times are GMT -5. The time now is 08:52 AM.