DuckDB Extension
Shellfs
Read shell-command output as a file in DuckDB, or pipe query results into another program. Use Unix pipelines with read_csv, read_json, and COPY.
On this page
Use a command’s output as a SQL data source
Shellfs connects DuckDB’s file readers and writers to Unix processes. A command can produce the bytes that a reader consumes, or receive the output written by DuckDB.
Use it with an existing command-line tool for fetching, decompressing, or transforming data. The pipe carries bytes, so its format still needs to match the DuckDB reader or writer. Commands run on the machine hosting DuckDB, which makes this a native-client workflow.
Example
Read CSV from a shell command
The printf command emits a header and two rows. A trailing pipe tells Shellfs to read the command’s standard output.
Before you run it: Use a supported native build with a Unix shell and printf available.
Tested with DuckDB 1.5.6. Run this example in the DuckDB CLI or a native client.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL shellfs FROM query_farm;
LOAD shellfs FROM query_farm;
SELECT *
FROM read_csv('printf "name,score\nMina,8\nTheo,9\n" |', header = true);
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://haybarn-extensions.query.farm';
INSTALL shellfs FROM query_farm;
LOAD shellfs FROM query_farm;
SELECT *
FROM read_csv('printf "name,score\nMina,8\nTheo,9\n" |', header = true);
INSTALL shellfs FROM community;
LOAD shellfs;
SELECT *
FROM read_csv('printf "name,score\nMina,8\nTheo,9\n" |', header = true);
Expected result
| name | score |
|---|---|
| Mina | 8 |
| Theo | 9 |
Shellfs executes the command in the DuckDB process’s environment. Only use commands you intend to run, and do not build shell strings from untrusted row values.
Common tasks
Deep Dive
Technical Details
Architecture
The Shellfs extension registers a DuckDB FileSystem subclass that recognizes two patterns:
- Path ending in
|— the extension runs the command and reads bytes from its stdout. - Path beginning with
|— the extension runs the command and writes bytes to its stdin.
On Linux and macOS the command runs as /bin/sh -c <command>, started with posix_spawn so the child gets the default SIGPIPE handling; the pipe is read and written unbuffered. On Windows the command runs through _popen (cmd.exe) in binary mode, so Parquet and other binary output passes through unchanged.
Everything else goes to the next filesystem in the chain (local disk, httpfs, aws, etc. as usual). The integration is one sentence of glue: any DuckDB function that takes a filename — read_csv, read_json, read_parquet, COPY ... TO, COPY ... FROM — accepts pipe paths transparently.
Why this exists
DuckDB ships with a strong built-in toolbox — HTTP, S3, parquet, JSON, etc. But the long tail is endless: a private API with custom auth, a tool that emits a proprietary text format, a one-off script that walks a directory tree. Rather than building a DuckDB extension for each, the Shellfs extension lets the existing Unix toolchain be the adapter.
Performance
Pipes are streaming — DuckDB starts processing rows as soon as the first chunk arrives, not after the upstream command finishes. For a seq 1 1_000_000_000 | you can LIMIT 100 and the upstream seq is killed via SIGPIPE without producing the rest.
Process overhead per pipeline is small (one spawn of /bin/sh). The bottleneck is whatever your shell pipeline is doing, not the bridge.
Exit status and early exits
The command’s exit status is checked when the pipe closes. A command that exits with a non-zero status raises an IO Error (Pipe process exited abnormally code=2: ...), whether DuckDB is reading from it or writing to it.
For a command DuckDB reads from, list other acceptable exit codes in an {allowed_exit_codes=...} suffix just before the final |. grep exits with status 1 when nothing matches, for example:
SELECT count(*) AS matchesFROM read_csv('(echo n; seq 1 20 | grep 99) {allowed_exit_codes=0,1}|');The codes are comma-separated non-negative integers, and listing them replaces the default of 0. The suffix is only recognized on read pipes.
When writing, a command that exits before reading all of its input (for example COPY ... TO '| head -n 3') closes the pipe early. By default that is an error (Could not write to pipe "| head -n 3": Broken pipe); it does not kill the DuckDB process. Set ignore_sigpipe to treat it as success instead:
SET ignore_sigpipe = true;COPY (SELECT * FROM range(1000000) t(n)) TO '| head -n 3' (FORMAT CSV, HEADER);With ignore_sigpipe on, the rest of the output is discarded and the command’s exit status is still checked. The setting is read for each pipe as it is opened.
Stopping early and cancelling
When DuckDB stops reading before the end of a command’s output — a LIMIT, for example — it closes the pipe. A command that is still writing exits with SIGPIPE, which is not an error. In the DuckDB 2.0 build, a command that keeps running anyway (one that ignores SIGPIPE, or is busy with something else) gets one second to exit. After that it receives SIGTERM, then SIGKILL one second later. The query never waits for it indefinitely:
-- `yes` never ends; DuckDB reads one row and the command is stoppedSELECT * FROM read_csv('yes |', header = false) LIMIT 1;The same applies when a COPY ... TO '| command' fails partway through.
In the DuckDB 2.0 build, cancelling a query (Ctrl-C in the CLI, interrupt() in a client API) also works while DuckDB is waiting on a pipe: waiting for a command’s output, for room to write its input, or for the command to exit. The command is then terminated as above. On Windows, pipes still block until the command produces output or exits.
Compared to alternatives
copy ... from program 'cmd'in PostgreSQL — same idea, more cumbersome syntax. TheShellfsextension slips into any DuckDB filename context.- Python
subprocess+ DataFrames — much heavier setup; you give up DuckDB’s vectorized execution against the streamed data. - DuckDB built-ins — use those first. Reach for the
Shellfsextension only when the data lives behind a tool DuckDB can’t speak natively.
Security
The same rule that applies to os.system(user_input) in Python applies here: never build pipe paths from untrusted strings. In a multi-tenant or hosted DuckDB environment, consider not loading the Shellfs extension at all, or restricting which roles can use it. See OWASP — Command Injection for the threat model.
Turning shellfs off
In the DuckDB 2.0 build, the extension runs no command while enable_external_access is false. That includes paths that look like they are inside allowed_directories. DuckDB checks those with a prefix match, so a “file” such as '/tmp/x; other-command |' would otherwise pass. Earlier builds ran such a command.
SET enable_external_access = false;SELECT * FROM read_csv('echo 1 |');-- Permission Error: Cannot run shell command "echo 1 |": shellfs commands are-- disabled because enable_external_access is falseTo turn off shell commands while still allowing other external access (local files, HTTP), set shellfs_enabled to false. This setting only works in one direction: once it is false, SET shellfs_enabled = true and RESET shellfs_enabled are rejected. A plain SET affects the current connection. SET GLOBAL affects every connection that has not set it itself.
SET GLOBAL shellfs_enabled = false;For a hard guarantee, combine either setting with lock_configuration, or don’t load the extension.
Changes in the DuckDB 2.0 build
- Security: no command runs when
enable_external_accessisfalse, even insideallowed_directories(see Turning shellfs off). - New setting:
shellfs_enabled, a one-way switch that turns shell commands off. - Wrong direction is an error: writing to a path that ends in
|(COPY ... TO 'cmd |') now fails withCannot write to "cmd |": a path that ends with '|' is a command that DuckDB reads from. Reading a path that starts with|fails the same way. Earlier builds started the command in the wrong direction. - No hangs: queries waiting on a pipe can be cancelled, and a command that keeps running after DuckDB stops reading is terminated (see Stopping early and cancelling).
- Early close is never an error: stopping early (a
LIMIT) is not reported as a failure of the command, with or withoutignore_sigpipe. Earlier builds printedException in ShellFileHandle destructorto standard output. A non-zero exit status after DuckDB has read all of a command’s output is still an error. - Binary pipes on Windows: commands are opened in binary mode, so Parquet and other binary formats pass through without CRLF translation.
Reference
Extension Contents
Quick reference to all available functions and settings organized by category.
| Name | Type | Description |
|---|---|---|
|
Configuration
|
||
| ignore_sigpipe | Object type: Setting | Treat a pipe closed early by either side (EPIPE / SIGPIPE) as success instead of an error |
| shellfs_enabled | Object type: Setting | Allow shellfs to run shell commands ('command |' and '| command'). |
No extension contents match that search.
Configuration
Settings
Configure the shellfs extension behavior using these settings.
ignore_sigpipe
Treat a pipe closed early by either side (EPIPE / SIGPIPE) as success instead of an error
false
shellfs_enabled
Allow shellfs to run shell commands ('command |' and '| command'). Once set to false it cannot be enabled again
true
Practical Examples
Cookbook
Real-world recipes and patterns for common use cases.
Read a pipeline
-- Numbers 1..100 containing a "2"SELECT * FROM read_csv('seq 1 100 | grep 2 |');Chain multiple commands:
-- First two multiples of 7 in [1, 35]SELECT * FROM read_csv('seq 1 35 | awk "\$1 % 7 == 0" | head -n 2 |');Curl an HTTP API
For custom headers, query params, retries, or pre-processing — pipe through curl:
SELECT full_name, stargazers_countFROM read_json('curl -s https://api.github.com/repos/duckdb/duckdb |');For programmatic HTTP from SQL without shelling out, see the http_client extension.
Drive a custom script
Any program that prints CSV (or JSON, or Parquet, etc.) to stdout becomes a queryable source — useful when you need bespoke parsing, schema-shaping, or to wrap an API DuckDB doesn’t speak natively:
# test-csv.pyprint("counter1,counter2")for i in range(10_000_000): print(f"{i},{i}")SELECT COUNT(DISTINCT counter1) FROM read_csv('python3 ./test-csv.py |');Pipe out of DuckDB
The | prefix turns COPY into a streaming export:
-- Stream a query result through gzipCOPY (SELECT * FROM big_table) TO '| gzip > out.csv.gz' (FORMAT CSV, HEADER, COMPRESSION UNCOMPRESSED);DuckDB can infer gzip compression from the destination string ending in .gz. Set COMPRESSION UNCOMPRESSED when the pipe runs gzip, so the output is compressed exactly once.
-- Stream into a downstream programCOPY (SELECT * FROM events) TO '| ./load_into_kafka.sh';Platform Support
Compatibility
Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.
Quick Facts
Platforms
DuckDB 2.0.0
- Linux x86_64aarch64
- Linux (musl) Not available
- macOS Apple Silicon
- Windows x86_64x86_64 (MinGW)
- WASM Not available
Compiled binary sizes
| Platform | Architecture | Size |
|---|---|---|
| Linux | x86_64 | 3.96 MB |
| Linux | aarch64 | 3.32 MB |
| macOS | Apple Silicon | 2.81 MB |
| Windows | x86_64 | 10.20 MB |
| Windows | x86_64 (MinGW) | 17.79 MB |
Compressed download sizes for the release shown above.
DuckDB & Haybarn
Release calendar- DuckDB v2.0.0 Query.Farm repository Supported
- Haybarn v1.5.6 Built-in community repository Supported
Browser examples use Haybarn-Wasm 2.0.0-rc12.
Checked .
Learn and explore
Tutorials and articles
Writing about Shellfs: how it works, what changed, and why.
-
Shellfs for DuckDB 2.0: pipes that stop when the query stops
Shellfs now interrupts stalled pipes on POSIX, cleans up abandoned commands, respects external-access settings, and handles broken pipes without killing DuckDB.