Explain What Happens When a Linux Shell Runs ls

Read the full interview experience this question came from →

Quick Overview

Trace a Linux shell's execution of ls through command lookup, process creation, directory reads, formatting, output, and return to the prompt.

Explain What Happens When a Linux Shell Runs ls

Company: Deshaw

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

What happens after a user enters `ls` in a Linux shell and presses Enter, until the directory listing and the next shell prompt appear? Describe a typical interactive shell executing an external `ls` program. State where aliases, functions, shell implementation, executable format, or command options can change the path. An exact syscall trace for an unspecified machine is not required. ### What a Strong Answer Covers - Shell parsing and command resolution before program execution. - Process creation versus replacement of a process image. - Arguments, environment, current directory, and standard file descriptors. - Directory enumeration, optional metadata reads, formatting, and output. - Program exit, the shell's wait, and representative failure points. ### Follow-up Questions - How does redirecting `ls` output to a file change the path? - Why can `ls -l` require more work than a simple listing of names?

Overview: Trace a Linux shell's execution of ls through command lookup, process creation, directory reads, formatting, output, and return to the prompt.

Read the full Deshaw Software Engineer interview experience this question came from

Community answers

Answer by freny07pethelis1996

`Flow diagram flowchart TD A[You type ls and press Enter] --> B[Shell reads the line from the terminal] B --> C[Shell parses the text] C --> D{Alias or shell function named ls?} D -->|Yes| E[Run alias expansion or function inside the shell] E --> Z[Shell prints next prompt] D -->|No| F[Look up ls in PATH e.g. /usr/bin/ls] F --> G{Found executable?} G -->|No| H[Print command not found] H --> Z G -->|Yes| I[fork: create a child process] I --> J[Child: optionally set up redirects] J --> K[Child: execve replaces itself with the ls program] K --> L[ls inherits cwd, env, stdin/stdout/stderr] L --> M[ls reads directory entries getdents] M --> N{Need more info? e.g. ls -l} N -->|Yes| O[stat each file for size, mode, owner...] N -->|No| P[Format names as text] O --> P P --> Q[write listing to stdout usually the screen] Q --> R[ls exits with a status code] R --> S[Parent shell waitpid wakes up] S --> Z Z --> T[Ready for your next command] BEFORE exec AFTER exec ┌─────────────┐ ┌─────────────┐ │ shell │ waits │ shell │ waits └──────┬──────┘ └──────┬──────┘ │ fork │ ┌──────▼──────┐ ┌──────▼──────┐ │ child │ still shell │ child │ now ls │ (copy) │ code briefly │ (ls image) │ └─────────────┘ └─────────────┘ The shell stays alive. Only the child becomes ls. Layman version (with the important guts) Think of the shell as a receptionist that never leaves the desk. When you ask for ls, it usually hires a helper, waits for the helper to finish, then says “what next?” You press Enter Your keyboar
|Home/Software Engineering Fundamentals/Deshaw
Deshaw logo
Deshaw
Aug 25, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

What happens after a user enters ls in a Linux shell and presses Enter, until the directory listing and the next shell prompt appear?

Describe a typical interactive shell executing an external ls program. State where aliases, functions, shell implementation, executable format, or command options can change the path. An exact syscall trace for an unspecified machine is not required.

What a Strong Answer Covers Guidance

  • Shell parsing and command resolution before program execution.
  • Process creation versus replacement of a process image.
  • Arguments, environment, current directory, and standard file descriptors.
  • Directory enumeration, optional metadata reads, formatting, and output.
  • Program exit, the shell's wait, and representative failure points.

Follow-up Questions Guidance

  • How does redirecting ls output to a file change the path?
  • Why can ls -l require more work than a simple listing of names?
Loading comments...