Excellent question. You've perfectly articulated a common set of frustrations that many developers and system administrators feel when their shell scripts grow beyond a few simple lines. The core issue is that traditional shells like bash are designed primarily as command interpreters, not as robust programming languages.
You are looking for a solution that bridges the gap, offering the interactive convenience of a shell with the power and safety of a modern programming language.
Here are my top recommendations, broken down into categories that directly address your pain points.
Category 1: The Next-Generation Shells
These are projects that fundamentally rethink what a shell should be. Instead of passing streams of text, they pass structured data (like objects or tables) between commands. This single change solves most of your problems.
1. Nushell (Nu)
This is likely the closest match to what you're asking for. Nushell's philosophy is "everything is data". It's a modern, cross-platform shell written in Rust.
# A simple Nu command
# It gets a list of files, finds the biggest 3, selects their name and size,
# and displays them as a formatted table. All of this is done with typed data.
ls | sort-by size --reverse | first 3 | select name size
How Nushell solves your problems:
- Math: Has built-in support for decimals (floating-point) and integers, as well as types for file sizes, dates, and durations. Math is natural:
let x = 5.5 / 2; echo $x outputs 2.75.
- Error Handling / Types: This is its greatest strength. Commands output tables of data with typed columns (string, int, bool, date, etc.). If you pipe a command's output into another that expects a number and it gets a string, it will raise a clear, immediate error instead of silently failing or misbehaving. This completely prevents the "text instead of numbers" problem.
- Functions: You can define custom commands (functions) with typed parameters. This makes them predictable and easier to reason about. Since the output is also structured, verifying a function's behavior is as simple as checking the output table's structure and values.
- Arrays/Data Structures: It doesn't just have arrays (called "Lists"); it has Tables (lists of records) as a primary data type. You can access columns, rows, and cells with an intuitive syntax. No more
IFS madness.
Why you'll like it: It keeps the familiar | for piping and has concise command names. It feels like a shell, but operates like a data-aware programming language.
Potential downsides: The ecosystem of built-in commands and plugins is still growing compared to the decades of bash tools. However, it can run external commands (git, curl, etc.) just fine, automatically trying to parse their text output into tables.
2. PowerShell (pwsh)
Originally a Windows-only shell, PowerShell is now fully cross-platform (Linux, macOS, Windows) and open-source. Like Nushell, its core principle is an object-based pipeline.
# Get the top 3 processes by memory usage
Get-Process | Sort-Object -Property WS -Descending | Select-Object -First 3 -Property ProcessName, WS
How PowerShell solves your problems:
- Math: It's built on the .NET runtime, giving it access to a full suite of numeric types (int, double, decimal) and mathematical functions.
- Error Handling / Types: Commands (called "cmdlets") output .NET objects, not text. When you pipe
Get-Process to Sort-Object, you are piping a collection of Process objects. Sort-Object can then access properties like WS (Working Set memory) directly. This strong typing prevents a massive class of errors. It also has robust try...catch...finally error handling.
- Functions: Functions are first-class citizens. You can define them with typed parameters, create modules, and there's a mature testing framework called Pester specifically for PowerShell.
- Arrays/Data Structures: Has excellent support for arrays (
@()) and hash tables (@{}), which are essential for any non-trivial scripting.
Why you'll like it: It's incredibly powerful, consistent (due to its Verb-Noun command naming convention), and extremely well-documented. If you ever work in a mixed Windows/Linux environment, it's the undisputed champion.
Potential downsides: The syntax can feel verbose to users accustomed to bash (Get-ChildItem instead of ls). It has a steeper learning curve.
Category 2: Using a Scripting Language as a Shell
This approach involves using a general-purpose programming language and a library that provides shell-like syntax for running external commands.
3. Python with plumbum or sh
If you already know and love Python, this is a fantastic option. You get the entire Python ecosystem (its libraries, testing frameworks, and language features) while writing code that looks like a shell script.
The best library for this is plumbum.
#!/usr/bin/env python3
from plumbum.cmd import ls, grep, wc
# Create a command chain using the pipe operator
chain = ls["-a"] | grep["\\.py"] | wc["-l"]
# Execute and get the output
print(f"There are {chain()} Python files here.")
# Example with error handling and data processing
from plumbum import local
import json
try:
# Run a curl command, get the result as a string
curl = local["curl"]
json_output = curl("https://api.github.com/users/tomerfiliba/repos")
# Use Python's powerful libraries to parse it
data = json.loads(json_output)
# Use Python's rich features
for repo in sorted(data, key=lambda r: r["stargazers_count"], reverse=True):
print(f"{repo['name']:<25} ⭐ {repo['stargazers_count']}")
except Exception as e:
print(f"An error occurred: {e}")
How Python/plumbum solves your problems:
- Math: You have all of Python's numeric capabilities, including the
decimal and math libraries.
- Error Handling / Types: You use Python's
try...except blocks for robust error handling. When a command fails, it raises an exception. You process data using Python's types, so you have full control.
- Functions: They are just Python functions. You can test them with mature frameworks like
pytest. This is a huge win for script reliability.
- Arrays/Data Structures: You have Python's lists, dictionaries, sets, and a universe of libraries for handling any data structure you can imagine.
Why you'll like it: You don't have to learn a new language. You can seamlessly mix shell commands with complex logic, API calls, and data manipulation in a single, clean script.
Potential downsides: It's not an interactive shell. You write a .py script and execute it. For an interactive experience, you can use IPython, which has excellent shell integration features (!ls, output = !ls -l, etc.).
Summary and Recommendation
| Feature | bash (The Problem) | Nushell (Recommended) | PowerShell (Recommended) | Python + plumbum |
|---|
| Math | Integer only | Full-featured | Full-featured | Full-featured |
| Data Pipeline | Raw Text | Structured Data (Tables) | Objects | Text, but easily parsed |
| Error Handling | Clumsy, often silent | Strongly-typed, clear errors | Exceptions, try/catch | Exceptions, try/catch |
| Functions | Hard to test | Typed, testable | Typed, testable (Pester) | Fully testable (pytest) |
| Arrays | Clunky strings | First-class Lists & Tables | First-class Arrays & Objects | First-class Lists & Dicts |
| Best For... | Quick, simple tasks | A truly modern shell experience | Sysadmins, mixed envs | Python devs automating tasks |
My direct recommendation for you is to start with Nushell.
It most directly addresses your desire for a tool that feels like a shell but acts like a programming language. The learning curve is gentle, and the benefits of a structured data pipeline are immediate and profound. It is the most "in the spirit of the shell" while fixing its fundamental flaws.
If you are already a heavy Python user, then using Python with plumbum for your complex scripts is a fantastic and pragmatic choice that leverages your existing skills.