How to make a Windows shortcut that runs a Cygwin Bash script that runs X11 programs

ArielMT's use case was a one-button access to rdesktop (because Microsoft's remote desktop application doesn't respect keyboard localization) in Cygwin. Just click a shortcut button on the QuickLaunch Toolbar, and up pops a Cygwin/X window without any other windows cluttering up the screen.

The things we need to do are:

  1. Configure Cygwin to start the X server on login without launching a terminal window as well,
  2. Write the script that launches the GUI applications you want,
  3. Create the shortcut to run that script as a client of the X server,

Configure Cygwin to start the X server on login

If you use the Cygwin setup program to install the X server, you should have shortcuts in your Start Menu under "Cygwin-X" for "XWin Server". If you have this icon, right-click on it, choose "Copy" from the pop-up menu, right-click on the "Startup" menu in the Start Menu, and choose "Paste" from the pop-up menu. If you don't have this icon but you do have Cygwin's X server installed:

  1. Navigate to the "Startup" menu in the Start Menu (under "All Programs" or "Programs").
  2. Right-click on the menu (not on any icons/items in the menu) and choose "Open" from the pop-up menu.
  3. In the window that opens, right-click on an empty part, where icons would be if there were enough, and choose "New" -> "Shortcut" from the pop-up menu.
  4. For the location of the item, type the following and click Next:
    • C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
  5. For the name for this shortcut, type "XWin Server" and click Finish.
  6. Right-click on the new shortcut and choose "Properties" from the pop-up menu.
  7. Click on the "Change Icon" button.
  8. Replace "run.exe" with "XWin.exe" in the "Look for icons in this file" box, and press the Enter key until the "Change Icon" window closes.
  9. Click on the OK button.

Configure the X server to stop starting up an xterm window

  1. Start a terminal emulator in Cygwin, whether it's Cygwin bash shell, mintty, xterm, or something else.
  2. Type the following command at a shell prompt and press Enter:
    • touch ~/.startxwinrc

Write the shell script that launches the X11 program(s)

Write a shell script that runs the command you would normally type into an xterm window, with the following changes:

  1. Place the following line as the first line of your shell script to tell the shell that bash should run it:
    • #!/bin/bash
  2. Prefix the command with "nohup ", a program which makes commands immune to hangups (being killed when the controlling terminal exits).
    • Run the command "man 1 nohup" if you want to learn more about the nohup command.
  3. Suffix the command with " &" to place the command in the background.
    • The command will execute in the foreground otherwise. See the bash manpage (type "man 1 bash") for more info.
  4. Follow your command or commands with the following line at the end of the script to give the X program time enough to start up:
    • sleep 3s
  5. Save your script in your home directory.
    • For the sake of example, this tutorial assumes /home/arielmt/x-foo-script

Create the shortcut that runs your shell script

If you want the shortcut to be an icon in your QuickLaunch Toolbar, then right-click on an empty part of the toolbar and choose "Open Folder" from the pop-up menu.

If you want the shortcut to be an item in your Start Menu, then navigate to the menu where you want the item to appear, right-click on the menu (not any items in the menu), and choose "Open" from the pop-up menu.

Otherwise, use Windows Explorer or File Explorer to navigate to where you want your shortcut to live.

Choose "File" -> "New" -> "Shortcut" from the menu bar, or right-click on an empty part of the window and choose "New" -> "Shortcut" from the pop-up menu.

For the location of the item, type the following, substituting your script name for /home/arielmt/x-foo-script:

  • C:\cygwin\bin\run.exe -p /usr/X11R6/bin xterm -display -e /bin/bash -l /home/arielmt/x-foo-script
    • Run.exe sets the path for xterm's benefit via the -p switch.
    • xterm uses Display 0, Screen 0, on the local machine, where your XWin server is running, with the -display switch.
    • xterm uses the rest of the shortcut name after the -e switch as the command it should run.
    • bash runs as a login shell, ensuring your normal environment including your PATH setting is present, with the -l switch.
    • bash runs your script (here /home/arielmt/x-foo-script), in xterm, in the X server, in Windows.

Click Next. For the name of the shortcut, give a meaningful name. Click Finish.

Right-click on your new shortcut, and choose "Properties" from the pop-up menu.

Type a meaningful pop-up tooltip in the "Comment" box.

Click on the "Change Icon" button, browse to the program, DLL file, or ICO file that has the icon you want to use, and choose the icon.

Click on the OK button.

The result

You'll still get an xterm window appearing, but it will disappear on its own after three seconds. The windows started from your script running in it, however, won't disappear until you make them.