#!/usr/bin/env bash # ${0} is the script name so this will output # to the current directory as script_name.log LOG_FILE="./${0}.log" # Log all output to file # tee -a - append # tee -i - ignore interrupt signals # STDOUT exec > >(tee -ai ${LOG_FILE}) # STDERR exec 2> >(tee -ai ${LOG_FILE}) # Copy the username@hostname working directory # and command into the log file before the # command is run function echo_command () { # id -un - Output username # pwd - Print working directory # BASH_COMMAND - Command that's about to be run echo "$(id -un)@$(hostname) $(pwd) $ ${BASH_COMMAND}" >> ${LOG_FILE} } trap echo_command DEBUG date echo "Just echoed the date" date echo "Echoed it again"