if [[ -z "${DTACH_SUB}" ]]; then { setopt NULL_GLOB while true; do set -- $tmpdir/dtach* if [[ "$#" -gt 0 ]]; then # session exists, so show the menu { while true; do set -- $tmpdir/dtach* if [[ "$#" -eq 0 ]]; then # logout if all sessions terminated clear logout fi # list sessions set -- $tmpdir/dtach* if [[ "$#" -gt 0 ]]; then ls $tmpdir/dtach* | sed 's/.*\/dtach-//g' fi echo -n "> " read -A args case "${args[1]}" in "") break; ;; q) break; ;; *) DTACH_SUB=true dtach -A "$tmpdir/dtach-${args[1]}" -z zsh clear ;; esac done } clear logout else # create the main session DTACH_SUB=true dtach -A $tmpdir/dtach-1 -z zsh clear set -- $tmpdir/dtach* if [[ "$#" -eq 0 ]]; then # logout if all sessions terminated clear logout fi fi done } fi