bashexpand
Bash-style parameter expansion — including the advanced operators — over any variable source: maps, closures, the environment, or your own backend.
use expand;
use HashMap;
let mut vars = from;
assert_eq!;
assert_eq!;
assert_eq!;
# Ok::
Unlike shellexpand/subst (which stop at $var and ${var:-default}), this crate implements the full operator set: substrings, glob prefix/suffix removal, replacements, case modification, and @ transforms. Unlike embedding a full shell, it has zero dependencies, no unsafe, and no shell semantics you didn't ask for — no command substitution, no word splitting, no surprises.
Syntax cheat sheet
| Syntax | Meaning | Example → result |
|---|---|---|
$var, ${var} |
plain expansion | ${name} → world |
${var:-word} |
default if unset or empty | ${nope:-x} → x |
${var-word} |
default if unset only | ${empty-x} → `` |
${var:=word} |
default + assign (writable ctx) | ${n:=1} → 1, stores n=1 |
${var:+word} |
alternate if set | ${name:+yes} → yes |
${var:?msg} |
error if unset/empty | Error::Required |
${#var} |
length (chars) | ${#name} → 5 |
${!var} |
indirection | ptr=name → ${!ptr} → world |
${var:off:len} |
substring, negatives ok | ${path: -6} → tar.gz |
${var#pat} / ## |
strip shortest/longest prefix | ${path##*/} → tool.tar.gz |
${var%pat} / %% |
strip shortest/longest suffix | ${path%%.*} → /usr/local/bin/tool |
${var/p/r} / // |
replace first / all | ${path//\//_} |
${var/#p/r} / /% |
replace anchored prefix/suffix | |
${var^} ^^ , ,, |
case modification (+ optional glob) | ${name^^} → WORLD |
${var@U} @u @L |
upper / capitalize / lower | ${name@U} → WORLD |
${var@Q} |
shell-quote safely | ${f@Q} → 'my file' |
${var@E} |
expand \n \t \r \0 \\ |
|
\$ |
literal dollar |
Globs support *, ?, [a-z], [!a-z], \x. Operator words are recursively expanded (${a:-${b:-c}}).
Variable names are _ and alphanumerics and may start with a digit: $1 / ${1} look up the variable named 1 in your context, just as bash reads its positional parameters. Like bash ("cannot assign in this way"), assigning to such a name (${1:=x}) is an error.
Recipes
Inline closure — no map needed
use expand;
// Annotate `&str` so inference picks the Context impl.
let mut ctx = ;
assert_eq!;
# Ok::
Environment variables with defaults
use ;
let editor = expand?;
assert!;
# Ok::
Layered lookup: overrides → env → defaults
use ;
use HashMap;
let cli = from;
let defaults = from;
let mut ctx = cli.chain; // first hit wins; chains nest
assert_eq!;
# Ok::
Fallible backends (database, RPC, files)
use ;
#
let mut ctx = TryFn;
# let _ = expand;
Or implement Context directly for full control (writability, caching, name mangling) — it's two methods, one of them optional. See examples/custom_backend.rs.
Config templating with hard requirements
# use expand;
# use HashMap;
# let mut vars: = new;
let template = "data = ${DATA_DIR:?DATA_DIR is required}";
match expand
Path one-liners
| Task | Expression |
|---|---|
| basename | ${f##*/} |
| dirname | ${f%/*} |
| extension | ${f##*.} |
| strip extension | ${f%.*} |
| swap extension | ${f%.gz}.xz |
Read-only expansion over a shared &map
use expand;
use HashMap;
Untrusted templates
use Expander;
# use HashMap;
# let mut vars: = new;
# let user_template = "${greeting:-hello}";
// Tighten the nesting limit for hostile input; glob matching is already
// linear-time, and there is no command substitution to worry about.
let out = new.max_depth.expand?;
# Ok::
Non-goals
Arrays, positional/special parameter semantics ($@, $?), command substitution $(...), arithmetic $((...)), extglobs @(...), POSIX classes [[:alpha:]], and the @P/@A/@K/@a transforms. If you need bash-the-language, look at brush.
MSRV & safety
Rust 1.70+. #![forbid(unsafe_code)], zero dependencies. Depth-limited recursion and linear-time glob matching make it safe to run on untrusted template input.
License
MIT