Bash SHELL EXTENSION

Custom shell implementation that overrides command formatting behavior.
Part of a modular command execution framework.

📄 Overview

The Bash class inherits from the base Shell class and provides a specialized implementation for command argument formatting. Unlike traditional shell implementations that might concatenate the executable with its arguments, the Bash class delegates argument handling directly to the parent class logic, preventing any premature merging.

This design ensures that the executable path and arguments remain distinct until the final execution layer, offering more flexibility and correctness in complex command building scenarios.

🧬 Inheritance hierarchy: Shell (base class) → Bash
⚙️ Overrides: _format_command(executable, args)

🏛️ Class Definition

Class: Bash(Shell)

Module: shell_extensions.bash (inferred)
Access: Public | Base Class: Shell
Purpose: Implements a Bash-like command formatter that avoids merging the executable with its argument list.

The Bash class does not introduce new attributes; instead, it refines the internal command formatting routine. All standard shell operations (execution, environment handling) are inherited from the parent Shell class, while the command assembly logic is customized via the overridden method.

⚙️ Method: _format_command

_format_command(executable: str, args: list[str]) -> list[str]

Visibility: protected (convention) | Override: Yes (overrides Shell._format_command)
Returns: list[str] — The processed argument list, which is exactly the original args list unchanged.

📌 Signature: def _format_command(self, executable: str, args: list[str]) -> list[str]

The method is responsible for formatting the command before it is executed by the shell. In the base Shell class, this method might combine the executable path with the arguments into a single flat list. However, the Bash class deliberately bypasses that concatenation and returns only the arguments as they were received.

Key behavior: The executable parameter is ignored. The method returns the args list as-is. The base Shell class later handles the overall command construction, ensuring that the executable is properly integrated without being merged prematurely.

💡 Design rationale: By returning only the arguments and discarding the executable concatenation, the class delegates final command assembly to the parent’s execution logic. This prevents double‑quoting errors and preserves the original argument structure, which is essential for complex command lines with flags, pipes, or redirections.

🧠 Behavioral Logic

The _format_command method implements a clean separation of concerns:

AspectDescription
Executable handlingThe executable parameter is ignored — it is not merged into the returned list.
Argument propagationThe entire args list is returned without any transformation, filtering, or reordering.
Parent delegationBase Shell class is responsible for final command assembly (e.g., joining executable + formatted args).
Return typelist[str] — always identical to the input args list.

This approach ensures that the Bash class does not impose any custom formatting that would alter the argument sequence. The parent's execution engine will later prepend the executable appropriately, preserving POSIX shell semantics.

📚 Inheritance & Base Shell Contract

The abstract or concrete Shell class defines the following contract for _format_command:

Base method signature (Shell class)

def _format_command(self, executable: str, args: List[str]) -> List[str]
The base implementation might join [executable] + args. Bash overrides this to avoid merging.

Because the Bash class returns only args, the parent’s command execution routine will typically handle the final command vector as:
final_command = [executable] + self._format_command(executable, args) → which becomes [executable] + args.

🔄 Flow example:
Shell.execute("git", ["status", "--short"])
_format_command("git", ["status","--short"]) returns ["status","--short"]
→ Parent command builder: ["git", "status", "--short"] ✅ Correct.

💻 Usage Example (Conceptual)

Although the full execution environment is not shown, typical usage of the Bash class inside a shell framework:

from shell_base import Shell
class Bash(Shell):
    def _format_command(self, executable: str, args: list[str]) -> list[str]:
        # returns args untouched → base class handles executable separately
        return args

# hypothetical instantiation
bash_shell = Bash()
# internal execution call would invoke _format_command
formatted = bash_shell._format_command("ls", ["-la", "/home"])
print(formatted)   # Output: ['-la', '/home']
                

Note: The parent Shell class will combine the executable and the returned argument list, ensuring proper command execution.

📑 API Reference

Method / AttributeParametersReturn typeDescription
_format_command executable: str, args: list[str] list[str] Overrides parent to return the original args without merging the executable. The base Shell class later constructs the full command vector.
Inherited methods
execute(), run(), etc.
Varies by Shell implementation Depends on parent All other shell behavior (process execution, environment, I/O) is inherited from Shell and unchanged.

📝 Implementation Notes & Rationale

⚠️ Edge case behavior: If the base Shell class expects _format_command to return a list that includes the executable, the Bash override will cause a different command structure. Ensure the parent class correctly handles this — standard practice is that the parent adds the executable after formatting.