Skip to main content

Bash replacement library for Python

Project description

minishell

An idiot admires complexity, a genius admires simplicity

©️ Legend Terry A. Davis

I want to ditch Bash and replace all use cases of it with Python. I recently learned that there's no good alternative for Bash. You can argue that there is, but then compare what they can write in Bash and how unreadable it becomes with any of the traditional shell solutions out there.

Goals

  • minishell is going to be extremely simple
  • Compete with Bash only
  • Do not add features just because other libraries have them
  • Stay focused: execute commands and parse arguments, nothing more

Philosophy

Bash works because it's the shell. You can't replicate that. But you can make Python script writing as painless as Bash for everything else: better error handling, better argument parsing, better readability.

minishell provides just what you need:

  • shell(cmd, *args) - Run commands with full TTY support and optional argument escaping
  • shell[cmd, *args] - Same as previous, but capture output
  • shell.args[1], shell.args.named_argument - Parse CLI arguments automatically

That's it. No frameworks, no magic.

Quick Start

# Raw command
shell("ls -la | grep test")

# With escaped arguments (safe!)
filename = "file with spaces.txt"
shell("rm {}", filename)

# Capture output
output = shell["pwd"]
print("Current dir:", output)

# Arguments are auto-parsed
app_name = shell.args.app_name
if not app_name:
    shell.exit("Error: --app-name required")

API

shell(cmd, *args)

Execute a command with optional argument escaping.

  • cmd: Command string with optional {} placeholders
  • args: Arguments to format and escape
  • Returns exit code
shell("echo hello")           # raw
shell("echo {}", "hello")     # with escaping

shell[cmd, *args]

Execute command and capture output. Returns the output string.

output = shell["pwd"]
# With escaped arguments
output = shell["find {} -name {}", "/path", "*.py"]

shell.read(cmd, *args, exit_on_error=False)

Execute command and capture output with more control.

  • Returns tuple[output, exit_code]
  • Set exit_on_error=True to exit on non-zero code (usually it's better to use shell[cmd, *args])
output, code = shell.read("ls")
output, code = shell.read("find {} -name {}", "/path", "*.py", exit_on_error=True)

shell.args

Automatically parsed command-line arguments.

# Positional
print(shell.args[0])

# Named arguments
print(shell.args.config)
print(shell.args.verbose)

# Multiple values become lists
if isinstance(shell.args.file, list):
    for f in shell.args.file:
        print(f)

Installation

pip install minishell

Or just copy minishell.py to your project.

Why not Bash?

Feature Bash minishell
Argument parsing 50+ lines 3 lines
Variable syntax $VAR, ${VAR} f-strings
Error handling set -e (fragile) Explicit
Type hints None Full typing
Readability Medium High

Contributing

We need Bash specialists to provide code examples that are:

  • Cleaner/simpler in Bash than in Python with minishell
  • Real-world use cases we should optimize for

Alternatively, suggest improvements to keep the library minimal and focused. Pure critique without suggestions is also appreciated!

The bar for adding features: Does this save significant boilerplate compared to Bash?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minishell-2.0.13.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minishell-2.0.13-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file minishell-2.0.13.tar.gz.

File metadata

  • Download URL: minishell-2.0.13.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for minishell-2.0.13.tar.gz
Algorithm Hash digest
SHA256 5194e1d2eebcda608e249b15f27098ee786ac1e78dacf463028955a73ffa2928
MD5 727fb8be71d200de146f877e20ee7c64
BLAKE2b-256 089c2fd788d52a19b43d14050ed7d2313b37106d47c14f915da667e588d7a8fd

See more details on using hashes here.

File details

Details for the file minishell-2.0.13-py3-none-any.whl.

File metadata

  • Download URL: minishell-2.0.13-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for minishell-2.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 5ae86e0bd4e8fe14a8d42dc60e607d5c9583f9582dc5f3d84f2e815019c0e8f5
MD5 a2e3fd75b5d28f28db54cfde83891733
BLAKE2b-256 b849e0c3178fd9ba5d9c90d75d54b29a96c233133f42cc5897fa80155dc85999

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page