I've been using bash for about a year now, writing scripts, handling loops, and executing basic commands. However, I've always been curious about the behind-the-scenes process when I run a command in the terminal. Specifically, what is the difference between typing `bash -c "ls -la"` and just `ls -la`? Most importantly, I want to know what happens step by step when I execute a command. How does it all connect back to the Linux kernel? I've read various resources, but I can't find clear answers to this question.
4 Answers
When you run a command, the shell finds `bash` in your PATH and uses the `execve` function to execute it. When you use `bash -c "ls -la"`, bash starts a new instance, parses the command, and runs `ls`. Without the `-c`, it just executes the running instance of bash and calls `ls` directly.
Bash -c creates a new bash instance just for that command. Once it runs `ls`, that instance exits. Conversely, when you just type `ls -la`, it runs in the current instance without launching a new one.
Using `strace ls -la` will detail all the system calls made when executing that command, displaying the arguments passed to each call. While it might seem complex at first, consulting the man pages can help you understand the specifics behind each syscall.
Does strace log the operations after execution, or is it more of a real-time tracker?
If you're looking to understand how `ls` interacts with the system, you can use the command `strace`. This tool will show all the system calls made by a program as it runs. For example, you could run `strace ls -la` to see how the `ls` command navigates the filesystem. If you don't have `strace`, you can easily install it through your package manager. It's a great way to delve into Linux internals!
That's awesome! I didn't know strace worked with every command. Are there any exceptions?
Not really; it should work with all commands unless you're dealing with really low-level commands or those that don't make system calls. It's a helpful tool!

What's the difference between strace and ltrace?