I have this program that can run with both a text user interface and a graphical user interface.

It lacks any command line switch to force one or the other, rather I guess it somehow auto-detects whether we are in X or not (e.g. if I run it from a virtual terminal it enters its text mode, and if I run it from an X terminal emulator it opens a separate graphical window).

I'd like to force it into text mode and have it run inside the X terminal. How would I go about doing it?

up vote 40 down vote accepted

Usually just

unset DISPLAY

in command-line of the terminal. Some applications are smarter than that, and actually check permissions and type of the console versus pseudoterminal.

  • 7
    Unfortunately, some really obstinate software will assume DISPLAY=:0 if it's unset. I believe you can fix that by running it under a different user and using iptables to drop loopback X11, but that's pretty gross. – Kevin Oct 21 at 19:21
  • 2
    @Kevin maybe DISPLAY=invalid:0 ? – sourcejedi Oct 21 at 19:57
  • 2
    @PeterCordes or you can execute the command through env instead of a subshell: env -u DISPLAY emacs foo.c – pabouk Oct 22 at 11:19
  • 2
    @PeterCordes emacs does have a command line flag to disable the use of X. Just type emacs -nw. But if it didn't, you could instead use DISPLAY= emacs, which works as well. – kasperd Oct 22 at 11:34
  • 2
    DISPLAY=0.0.0.0:0 fails instantly without doing a name server lookup. – pts Oct 22 at 12:08

If you want to disable X for a single command you can write

DISPLAY= ./my_command

Notice the strategical blank space after =. More generally, you can set environment variables for a process by prefixing your command with a sequence of <variable>=<value> separated by spaces. Since space serves as separator, = immediately followed by a space clears the preceding variable. We can look at the effect of these prefixes by using a subshell as the command and then printing its environment. Take for instance:

$ A=a B=b C= D=d sh
$ echo $A $B $C $D

This will print

a b d

This shows that the environment of the subshell indeed is different as intended. Note that shell substitution happens before the individual arguments are passed to echo, so there's only a single space between b and d in the output, just as if the command line were echo a b d (with two spaces).

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.