set gnome terminal background/text color from bash script

Method #1 - Using dconf

Background

You can use the dconf tool to accomplish this, however it's a mult-step process.

DESCRIPTION
       The dconf program can perform various operations on a dconf database, 
       such as reading or writing individual values or entire directories.
       This tool operates directly on the dconf database and does not read 
       gsettings schema information.Therefore, it cannot perform type and 
       consistency checks on values. The gsettings(1) utility is an 
       alternative if such checks are needed.

Usage

$ dconf
error: no command specified

Usage:
  dconf COMMAND [ARGS...]

Commands:
  help              Show this information
  read              Read the value of a key
  list              List the contents of a dir
  write             Change the value of a key
  reset             Reset the value of a key or dir
  update            Update the system databases
  watch             Watch a path for changes
  dump              Dump an entire subpath to stdout
  load              Populate a subpath from stdin

Use 'dconf help COMMAND' to get detailed help.

General approach

  1. First you'll need to get a list of your gnome-terminal profiles.

    $ dconf list /org/gnome/terminal/legacy/profiles:/
    <profile id>
    
  2. Using this <profile id> you can then get a list of configurable settings

    $ dconf list /org/gnome/terminal/legacy/profiles:/<profile id>
    background-color
    default-size-columns
    use-theme-colors
    use-custom-default-size
    foreground-color
    use-system-font
    font
    
  3. You can then read the current colors of either the foreground or background

    foreground

    $ dconf read /org/gnome/terminal/legacy/profiles:/<profile id>/foreground-color
    'rgb(255,255,255)'
    

    background

    $ dconf read /org/gnome/terminal/legacy/profiles:/<profile id>/background-color
    'rgb(0,0,0)'
    
  4. You can change the colors as well

    foreground

    $ dconf write /org/gnome/terminal/legacy/profiles:/<profile id>/foreground-color "'rgb(255,255,255)'"
    

    background

    $ dconf write /org/gnome/terminal/legacy/profiles:/<profile id>/background-color "'rgb(0,0,0)'"
    

Example

  1. Get my profile ID

    $ dconf list /org/gnome/terminal/legacy/profiles:/
    :b1dcc9dd-5262-4d8d-a863-c897e6d979b9/
    
  2. Use the profile ID to get a list of settings

    $ dconf list /org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/
    background-color
    default-size-columns
    use-theme-colors
    use-custom-default-size
    foreground-color
    use-system-font
    font
    
  3. Change your background blue

    $ dconf write /org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/background-color "'rgb(0,0,255)'"
    

              ss #1

A Note on colors

You can use either the notation rgb(R,G,B) when specifying your colors or the hash notation #RRGGBB. In the both notations the arguments are red, green, and blue. The values in the first notation are integers ranging from 0-255 for R, G, or B. In the second notation the values are in hexidecimal ranging from 00 to FF for RR, GG, or BB.

When providing either of these to dconf you need to wrap it properly in double quotes with single quotes nested inside. Otherwise dconf will complain.

  • "'rgb(0,0,0)'"
  • "'#FFFFFF'"
  • etc.

Method #2 - Using gconftool-2

On my Ubuntu 12.04 system I was able to change the colors via the command line as follows.

NOTE: The options are ultimately stored in this file, $HOME/.gconf/apps/gnome-terminal/profiles/Default/%gconf.xml.

General approach

  1. First you'll need to get the tree for gnome-terminal's profile.

    $ gconftool-2 --get /apps/gnome-terminal/global/profile_list
    [Default]
    
  2. Using the resulting tree we can find out what attributes are configurable.

    $ gconftool-2 -a "/apps/gnome-terminal/profiles/Default" | grep color
     bold_color_same_as_fg = true
     bold_color = #000000000000
     background_color = #FFFFFFFFFFFF
     foreground_color = #000000000000
     use_theme_colors = false
    
  3. Get/Set the background_color & foreground_color attributes

    $ gconftool-2 --get "/apps/gnome-terminal/profiles/Default/foreground_color"
    #000000000000
    
    $ gconftool-2 --set "/apps/gnome-terminal/profiles/Default/background_color" --type string "#000000FFFFFF"    
    
  4. Confirm

    $ gconftool-2 -R /apps/gnome-terminal/profiles/Default | grep color
     bold_color_same_as_fg = true
     bold_color = #000000000000
     background_color = #000000FFFFFF
     foreground_color = #000000000000
     use_theme_colors = true
    

References

  • CHANGING TERMINAL PREFERENCES IN GNOME 3
  • base16-gnome-terminal / base16-tomorrow.light.sh
  • Is there a way to temporarily change the terminal colour?