Monadic shell scripts

Posted by: Seth Lakowske

Published:

Shell scripts can be monad like in their construction. When shell scripts are chained together with pipes, you get a sense of their composability. I'll walk you through a few examples to give you a concrete example of this composability.

Copies my .ssh directory to a remote computer

(cd /home/seth ; tar -cf - .ssh) | ssh [email protected] -c tar -xf -

tar is amazing! It maps a local filesystem to a stream that we can send to stdout, or in this case across the internet! At the other end of the pipe, tar maps our stream to the other local filesystem. Many programs use the filesystem as their environment, reading config files /etc, ~/.ssh or wherever. If you have a program that only relies on the filesystem, add tar then your program can take a stream as input too!

(cd /home/seth ; tar -cf - .ssh) | ssh [email protected] -c tar -xf -
alias k8s-keys='docker run -i lakowske/k8s-keys'
alias tout='tar -cf -'
alias tin='tar -xf -'