#!/bin/bash
export sleeptime=1.50000$RANDOM
# Previous line have to stay at line number 2!
# First 12 lines are only for adding delay to make demonstration readable

# Don't change: comments, not ended (ending with ``\'') and emtpy lines.
[ ! "$1" ] || [ "${1//nosleep}" ] && exec bash -v <( \
	sed -ue '2d;/^\(#.*\|\|.*\\\)$/!s/$/\nsleep '$sleeptime/ $0) \
	2> >( sed -ue "/^sleep $sleeptime/d" ) nosleep

# The real stuff to hack bash for StackOverflow begin there.
# Nota the part `;/sleep $sleeptime/d` is part of delaying and could be deleted.

# .1 Opening a new console for making ouput proper
exec 3> >(xterm -geom 80x26 -fs 8 -e "\
    sed -ue 's/^    /\t/;ta;s/^/    /;:a;/sleep $sleeptime/d' /proc/$$/fd/3"
)

# .2 Some preparations
LANG=C
BASH_XTRACEFD=3
PS4='\w$ '
# Don't try to redirect STDERR in same manner!
exec 1>&3
set -x

set -- $'\e[H\e[J    '
# This is the right way to redirect both to secondary console
ls -ld /tmp /tnt 2>&1
echo
cd /tmp
pwd
mkdir test
cd test
ls -al
cd ..
rmdir -v test

# That's all folks!
set +x
exec 1>&2
sleep 3
exec 3>&-
