Using env to invoke pkgx is typical for tools that
have no POSIX location.
The -S parameter is required to pass multiple arguments.
Including Additional pkgs
Scripts are the glue that allows open source to be composed into powerful new
tools. With our +pkg syntax you make anything in open source available to your
script.
#!/usr/bin/env -S pkgx +openssl deno run
Deno.dlopen("libssl.dylib")
Robustness requires precisely specifying your
environment:
#!/usr/bin/env -S pkgx bash>=4
source <(pkgx dev --shellcode)
# ^^ bash >=4 is required for this syntax, and eg macOS only comes with bash 3
Scripting for Various Languages & Their Dependencies
Python
Use uv to import PyPi dependencies:
#!/usr/bin/env -S pkgx +python@3.11 uv run --with requests<=3 --with rich
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
Ruby
#!/usr/bin/env -S pkgx ruby@3
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'ruby-macho', '~> 3'
end
JavaScript & TypeScript
#!/usr/bin/env -S pkgx deno@2 run
import fs from "npm:fs";
Rust, Go, C, C++, etc
#!/usr/bin/env -S pkgx +cargo scriptisto
# snip… type `pkgx scriptisto new cargo` for the rest.
Mash
We think pkgx scripting is so powerful that we made a whole package manager to
show it off.
Other Examples
We make use of pkgx scripting all over our repositories. Check them out!
Ultra Portable Scripts
Requiring a pkgx shebang is somewhat limiting. Instead you can use our cURL
one-liner coupled with +pkg syntax to temporarily install pkgs and utilize
them in your scripts:
#!/bin/bash
eval "$(sh <(curl https://pkgx.sh) +git)"
which git # prints soemthing like /tmp/pkgx/git-scm.org/v2.46.3/bin/git