#!/bin/bash
 
echo "BASH_VERSION=$BASH_VERSION"
 
shopt -s compat31 2>/dev/null || true
 
# Equivalent of grep -w, but using bash built-ins only.
matches_word () {
    local str="${1:-}"
    local re="\\b${2:-}\\b"
    [[ "$str" =~ $re ]] && return 0
    return 1
}
 
# Append values to string output if they are not already present.
append_if_not_present () {
    local base_str="${1:-}"; shift
    for add_str in "$@" ; do
        if ! matches_word "$base_str" "$add_str" ; then
            base_str="${base_str} ${add_str}"
        fi
    done
    echo "${base_str## }"
}
 
string="hello world this hellostring worldthis string"
 
echo $string
 
append_if_not_present "$string" world this_string orldthis string hello this thisworld this this thisworld oerigfhreo