Bash – How to eliminate annoyances when starting a GUI from a terminal

autocompletebash

I prefer to launch GUI applications from a terminal window rather than by using a graphical desktop. A frequent annoyance is that often the developers haven't anticipated this type of use, so the app prints lots of useless, cryptic, or uninformative messages to stdout or stderr. Further clutter on the terminal occurs because running the program in the background, with an &, generates reports of the creation and termination of the job.

What is a workaround for these problems that will accept command line arguments and handle autocompletion?

Related: https://stackoverflow.com/questions/7131670/make-bash-alias-that-takes-parameter

Best Answer

Redirecting the standard error immediately to /dev/null is a bad idea as it will hide early error messages, and failures may be hard to diagnostic. I suggest something like the following start-app zsh script:

#!/usr/bin/env zsh
coproc "$@" 2>&1
quit=$(($(date +%s)+5))
nlines=0
while [[ $((nlines++)) -lt 10 ]] && read -p -t 5 line
do
  [[ $(date +%s) -ge $quit ]] && break
  printf "[%s] %s\n" "$(date +%T)" "$line"
done &

Just run it with: start-app your_command argument ...

This script will output at most 10 lines of messages and for at most 5 seconds. Note however that if the application crashes immediately (e.g. due to a segmentation fault), you won't see any error message. Of course, you can modify this script in various ways to do what you want...

Note: To make completions work with start-app in zsh, it suffices to do:

compdef _precommand start-app

and in bash:

complete -F _command start-app

(copied from the one for exec and time in /usr/share/bash-completion/bash_completion).

Related Question