I have a variable that contains the following space separated entries.
variable=\"apple lemon papaya avocado lemon grapes papaya apple avocado mango banana\"
In pure, portable sh
:
words="apple lemon papaya avocado lemon grapes papaya apple avocado mango banana"
seen=
for word in $words; do
case $seen in
$word\ * | *\ $word | *\ $word\ * | $word)
# already seen
;;
*)
seen="$seen $word"
;;
esac
done
echo $seen
This pipeline version works by preserving the original order:
variable=$(echo "$variable" | tr ' ' '\n' | nl | sort -u -k2 | sort -n | cut -f2-)