#!/bin/bash
if ! tty -s; then
gnome-terminal --title="Some title" --window -- "${0}" "${@}"
exit
fi
echo "Press a key to close the window."
read xxx
The tty command returns with an exit code higher than 0 if the input isn't connected to terminal, hence it can be used to check whether a script is running in the background or not. Of course you could check for installed terminal emulators as well to make it compatible across multiple platforms/desktop environments and/or checking environment variables like XDG_CURRENT_DESKTOP to spawn the script in a fitting terminal emulator, but I usually don't do that for my scripts.