#!/bin/bash result=":" IFS=: for p in $1; do [[ "$result" == *:"$p":* ]] || result="${result}${p}:" done result="${result#:}" echo "${result%:}"