hildred hildred - 5 days ago 4x
Bash Question

Check for X11 extension

I am writing a shell script that needs to differ its behavior and provide different options to called programs based on the presence or absence of particular X11 extensions. I have a working solution, but I am hoping for a cleaner solution. I am open to considering a simple c program to do the test and return the result. Here is what I have working as a minimal functional example:

xdpyinfo |sed -nr '/^number of extensions/,/^[^ ]/s/^ *//p' | \
grep -q $EXTENSION && echo present

I think there is a way to simplify the sed,grep but I really would prefer not to parse


You have the C-tag, too, so let me suggest to do the xdpyinfo yourself. The following C program prints just the extensions:

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int compare(const void *a, const void *b)
  return strcmp(*(char **) a, *(char **) b);

static void print_extension_info(Display * dpy)
  int n = 0, i;
  char **extlist = XListExtensions(dpy, &n);

  printf("number of extensions:    %d\n", n);
  if (extlist) {
    qsort(extlist, n, sizeof(char *), compare);
    for (i = 0; i < n; i++) {

      printf("    %s\n", extlist[i]);

  // TODO: it might not be a good idea to free extlist, check

int main()
  Display *dpy;
  char *displayname = NULL;

  dpy = XOpenDisplay(displayname);
  if (!dpy) {
    fprintf(stderr, "Unable to open display \"%s\".\n",



Compile with e.g.: GCC

gcc -O3 -g3  -W -Wall -Wextra  xdpyinfo1.0.2.c  $(pkg-config --cflags --libs x11)  -o xdpyinfo1.0.2

(should give a warning about unused argc but that's harmless)

Just change the printf()'s to the format you want.