Bash SHELL EXTENSION
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.
_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.
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.
🧠 Behavioral Logic
The _format_command method implements a clean separation of concerns:
| Aspect | Description |
|---|---|
| Executable handling | The executable parameter is ignored — it is not merged into the returned list. |
| Argument propagation | The entire args list is returned without any transformation, filtering, or reordering. |
| Parent delegation | Base Shell class is responsible for final command assembly (e.g., joining executable + formatted args). |
| Return type | list[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.
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 / Attribute | Parameters | Return type | Description |
|---|---|---|---|
_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 methodsexecute(), 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
- No concatenation: The original code comment explicitly states: "We no longer concatenate [executable] + args here." This is a deliberate design shift.
- Delegation to parent: By returning only
args, the parentShellclass can handle the executable separately, preserving flexibility for different shell dialects (e.g., sh, zsh). - Why return args as-is? Prevents double-escaping issues and ensures that argument boundaries are respected. The executable is not treated as part of the argument list prematurely.
- Performance: The method is O(1) in terms of transformation — no copy or list mutation is performed (the same list reference is returned).
- Type safety: Annotations
executable: strandargs: list[str]improve maintainability and compatibility with static type checkers.
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.