How does bash process user input from the command line
Linux - NewbieThis 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
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.
...
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.
...
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.
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.
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)
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 ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.