dt

duct tape for your unix pipes



What's dt?

dt is a functional programming riff on shell one-liners. It's useful for many situations, its simple syntax makes it easy to write and read, and dt is for solving problems with no pretense of elegance. The philosophy can be summed up with quotes from the ultimate handyman, Red Green:

It's meant to supplement (not replace!) other tools like awk, sed, xargs, and shell built-ins. A functional programming riff on Python/Ruby/Perl one-liners, with extremely minimal syntax that's easy to read and easy to reason about.

dt is released as open source software, usable under the (permissive) terms of the BSD 3-Clause license.


Use in Pipes

When piping in/out, the REPL is skipped. If dt has receives standard input from a pipe, it's fed to `dt` as a list of lines.

          $ seq 3 | dt rev pls
          3
          2
          1
        

Great for aliases:

          $ alias scream-lines="dt [upcase words unlines] map pls"
          $ echo "hey you pikachu" | scream-lines
          HEY
          YOU
          PIKACHU
        

If you want to read lines manually, use `stream` as the first command:

          $ alias head.dt="dt stream [rl pl] args last to-int times"
          $ seq 100 | head.dt 3
          1
          2
          3
        

Use in Shebang Scripts

When the first argument to `dt` is a file starting with `#!` it will interpret the file. In short: `dt` supports shebang scripting.

A naive tee implementation:

tee.dt
          #!/usr/bin/env dt

          readln unlines   \stdin :
          args pop   \file :

          stdin pl
          stdin file writef
        

Then use like:

          cat wish-list | sed 's/red/green/g' | tee.dt new-wish-list
        

Using Interactive Mode (REPL)

Running `dt` by itself with no pipes in or out starts a read-eval-print loop (REPL).

          $ dt
          dt 1.x.x
          Now, this is only temporary... unless it works.
          » # Comments start with #
          »
          » 1 1 + println
          2
          »
          » # Printing is common, so there are a bunch of printing shorthands.
          » # "p" is print, "pl" is print line, "pls" is print lines (i.e. of a list of values)
          » # Let's define a command that consumes a value, prints it, then returns its double.
          »
          » [ \n :   n p " " p   n 2 *] \print-and-double def
          »
          »
          » # And let's do it... 7 times!
          »
          » 1 \print-and-double 7 times   drop
          1 2 4 8 16 32 64
          »
          »
          » # You can conditionally execute code
          »
          » ["hi" pl] false do?
          » ["bye" pl] true do?
          bye
          »
          » quit
        

For a better experience, also install rlwrap and set a shell alias like so:

          $ alias dtsh='rlwrap dt'
          $ dtsh
          dt 1.x.x
          Now, this is only temporary... unless it works.
          »
        

The above example assumes a bash-like shell. Details on the syntax and configuration files to set an alias that persists will vary by your shell.


To continue learning about dt, consider one of these options: