Simple Zenity script - to select icon?

I am trying to make a simple graphical Zenity+Bash script that will list all the icon themes available in /usr/share/icons, then let the user select one and click "OK" to replace gtk-icon-theme-name="Humanity" in ~/.gtkrc-2.0.mine to say the theme name they chose instead of Humanity. I am doing this to provide a graphical method of choosing your icon theme under Openbox, since there is no mainstream program for that yet.

This is what I have so far:

#!/bin/bash
ICONS=$(ls -I "*.*" /usr/share/icons); SELECTED=$(zenity --list --column=/usr/share/icons \ $ICONS); echo 'gtk-icon-theme-name="$SELECTED"' >~/.gtkrc-2.0.mine

It works, except that echo will actually write $SELECTED in the text file, instead of taking the argument and replacing it with the user's choice.

Thank you in advance to anyone who can help me learn to do this and Zenity basics.


Solution 1:

This is simple. If you want echo to print the value of $SELECT, you should not use single quotes ', do double quotes " instead. Followings are the meanings of quoting and backslash.

  • The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.Use the double quotes when you want only variables and command substitution.

    • Variable - Yes
    • Wildcards - No
    • Command substitution - yes
  • The single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.

    • Variable - No
    • Wildcards - No
    • Command substitution - No
  • Use backslash ('\') to change the special meaning of the characters or to escape special characters within the text such as quotation marks.

Thus your command should be:

#!/bin/bash
ICONS=$(ls -I "*.*" /usr/share/icons); SELECTED=$(zenity --list --column=/usr/share/icons \ $ICONS); echo "gtk-icon-theme-name=\"$SELECTED\"" >~/.gtkrc-2.0.mine

Solution 2:

In addition to @TungTran's answer, your script begs for some improvements.

Cramming the script on a single line makes it hard to read/understand/debug. Write one command per line:

#!/bin/bash
ICONS=$(ls -I "*.*" /usr/share/icons)
SELECTED=$(zenity --list --column=/usr/share/icons \ $ICONS)
echo "gtk-icon-theme-name=\"$SELECTED\"" >~/.gtkrc-2.0.mine

Another sometimes less troublesome way to embed a variable in text output is using printf:

printf 'gtk-icon-theme-name="%s"\n' "$SELECTED" >~/.gtkrc-2.0.mine