Elegant and efficient way to start GUI programs from terminal without spamming it (Bash or any Posix shell) Elegant and efficient way to start GUI programs from terminal without spamming it (Bash or any Posix shell) shell shell

Elegant and efficient way to start GUI programs from terminal without spamming it (Bash or any Posix shell)


As others have pointed in the comments, I think the real problem is a way to distinguish between gui vs. non-gui apps on the commandline. As such, the cleanest way I could think of is to put this part of your script:

  #!/bin/bash  if [ $# -gt 0 ] ; then    ($@ &) &>/dev/null  else    echo "missing argument"  fi

into a file called gui, then chmod +x it and put it in your ~/bin/ (make sure ~/bin is in your $PATH). Now you can launch gui apps with:

`gui google-chrome`

on the prompt.

Alternatively, you can do the above, then make use of bind:

bind 'RETURN: "\e[1~gui \e[4~\n"'

This will allow you to just do:

google-chrome

on the prompt and it would automatically append gui before google-chrome

Or, you can bind the above action to F12 instead of RETURN with

bind '"\e[24~": "\e[1~gui \e[4~\n"'

To separate what you want launched with gui vs. non-gui.

More discussion on binding here and here.

These alternatives offer you a way out of endless aliases; a mix of:

  • Putting gui in your ~/bin/, and
  • Binding use of gui to F12 as shown above

seems the most ideal (albeit hacky) solution.


Update - @Enno Weichert's Resultant Solution:

Rounding this solution out ...

This would take care of aliases (in a somewhat whacky way though) and different escape encodings (in a more pragmatic rather than exhaustive way).

Put this in $(HOME)/bin/quiet

#!/bin/bash -iif [ $# -gt 0 ] ; then  # Expand if $1 is an alias  if [ $(alias -p | awk -F "[ =]" '{print $2}' | grep -x $1) > 0 ] ; then    set -- $(alias $1 | awk -F "['']" '{print $2}') "${@:2}"  fi  ($@ &) &>/dev/nullelse  echo "missing argument"fi

And this in $(HOME)/.inputrc

## Bind prepend `quiet ` to [ALT][RETURN]## The condition is of limited use actually but serves to seperate# TTY instances from Gnome Terminal instances for me.# There might very well be other VT emulators that ID as `xterm`# but use totally different escape codes!#$if $term=xterm  "\e\C-j": "\eOHquiet \eOF\n"$else  "\e\C-m": "\e[1~quiet \e[4~\n"$endif


Although it is an ugly hack, I sometimes use nohup for a similar needs. It has the side effect of redirecting the command's output, and it makes the program independent of the terminal session.

For the case of running GUI programs in a desktop envinroment it has only little risk for resource leaks as the program will anyway end with window manager session. Nevertheless it should be taken into account.

There is also the option of opening another terminal session. In Gnome for instance, you can use gnome-terminal --comand 'yourapp'.
But this will result in opening many useless terminal windows.


First, it's a great idea to launch GUI applications from the terminal, as this reduces mouse usage. It's faster, and more convenient in terms of options and arguments. For example, take the browser. Say you have the URL in the clipboard, ready to paste. Just type, say, ice (for Iceweasel) and hit Shift-Insert (to paste) and Enter. Compare this to clicking an icon (possibly in a menu), wait for the window to load (even worse if there is a start up page), then click the URL bar (or hit Ctrl-L), then Ctrl-V... So I understand you desire for this to work.

But, I don't see how this would require an "infinite" list of aliases and functions. Are you really using that many GUI applications? And even so, aliases are one line - functions, which may be more practical for handling arguments, are perhaps 1-5 lines of (sparse) code. And, don't feel you need to set them up once and for all - set them up one by one as you go along, when the need arises. Before long, you'll have them all.

Also, if you have a tabbed terminal, like urxvt (there is a Perl extension), you'd benefit from moving from "GUI:s" to "CLI:s": for downloading, there's rtorrent; for IRC, irssi; instead of XEmacs (or emacs), emacs -nw; there is a CLI interface to vlc for streaming music; for mail, Emacs' rmail; etc. etc.! Go hunt :)

(The only sad exception I've run across that I think is a lost cause is the browser. Lynx, W3M, etc., may be great from a technical perspective, but that won't always even matter, as modern web pages are simply not designed with those, text-only browsers in mind. In all honesty, a lot of those pages look a lot less clear in those browsers.)

Hint: To get the most out of a tabbed terminal, you'd like the "change tab" shortcuts "close" (e.g., Alt-J for previous tab and Alt-K for next tab, not the arrow keys that'll make you reach).

Last, one solution that'll circumvent this problem, and that is to launch the "GUI:s" as background processes (with &) in your ~/.xinitrc (including your terminal emulator). Not very flexible, but great for the stuff you always use, every time you use your computer.