logo TeddyDD

Trivial, Dumb Shell Function I Use Daily

Even simplest kind of automation can be great productivity boost. Let me show you a Fish shell function I’v been using daily.

function gtmp --description 'open shell in ephemeral directory'
	set -l t (mktemp -d -t quic-tmp.XXXXX)
	pushd $t
	$SHELL
	popd
	rm -rf $t
end

I wrote it in Fish since I had no intention of sharing. It roughly translates to following sh script:

#!/bin/sh
set -euf

dir=$(mktemp -d -t quick-tmp.XXXX)

cleanup() {
	rm -rf "$dir"
}

trap 'cleanup; trap - EXIT' EXIT INT HUP

(
	cd "$dir" || exit 1
	$SHELL
)

The gtmp command starts a new sub-shell in temporary directory. When shell exits the directory is deleted.

Why this is useful? It allows me to clone git repo to take a peek without bloating my hard drive. It lets me conduct tiny experiments with UNIX tools and files without thinking about „where should I store these files”. I can play around without mental overhead of deciding where throwaway files should be placed.

Published: