#!/usr/bin/env bash # echo and eval set -e a= while getopts a OPT; do case "$OPT" in a) # Append to file instead of overwriting. a=-a ;; ?) exit 2 ;; esac done shift "$(($OPTIND - 1))" cmd="$1" outfile="${2:-/dev/null}" mkdir -p "$(dirname "$outfile")" echo "$cmd" | tee $a "$outfile" eval "$cmd" exit "$?"