Append to a dconf list and avoiding duplicates with existing entries

I need to do this for a script i'm writing to add custom keyboard shortcuts. I get the existing list with

gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/']

and now i need to add new entries to it and avoid duplicates with any previous custom$ shortcuts.

gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[<altered_list>]"

Adding values to an existing array

arr=( "${arr[@]}" "new_element1" "new_element2" "..." "new_elementN")
#Or
arr+=( "new_element1" "new_element2" "..." "new_elementN" )

using command substituion

CUSTOM_SHORTCUTS_LIST=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
# ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/']
echo -e "\nCUSTOM_SHORTCUTS_LIST: \n${CUSTOM_SHORTCUTS_LIST}"

NEW_SHORTCUTS_LIST="${CUSTOM_SHORTCUTS_LIST%]*}, 'blah']" # substituion

echo -e "\nNEW_SHORTCUTS_LIST: \n${NEW_SHORTCUTS_LIST}\n"
# ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', 'blah']

Here are some way people have done this when creating Gnome Terminal themes

# One Dark
# https://github.com/denysdovhan/one-gnome-terminal/blob/master/one-dark.sh
dlist_append() {
    local key="$1"; shift
    local val="$1"; shift

    local entries="$(
        {
            "$DCONF" read "$key" | tr -d '[]' | tr , "\n" | fgrep -v "$val"
            echo "'$val'"
        } | head -c-1 | tr "\n" ,
    )"

    "$DCONF" write "$key" "[$entries]"
}
# Nord
# https://github.com/arcticicestudio/nord-gnome-terminal/blob/develop/src/nord.sh
append_profile_uuid_to_list() {
  local uuid list
  uuid="$1"
  list=$(gsettings get "$GSETTINGS_PROFILELIST_PATH" list)
  gsettings set "$GSETTINGS_PROFILELIST_PATH" list "${list%]*}, '$uuid']"
}

Links

Please note that this site and the posts on it are, and will always be, a work in progress. If i waited for perfection, i’d never get anything done.