Bash (Scripting) - remove duplicate values from a variable

by
Jeremy Canfield |
Updated: March 08 2020
| Bash (Scripting) articles
Let's say you have a variable (foo in this example) with dupliate values (bar in this example).
foo="hello world bar bar bar bar bar"
The following command will remove the duplicate values from the variable (credit goes to this Stack Overflow post).
echo "$foo" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s",$i,FS)}{printf("\n")}'
The result will be:
hello world bar
Did you find this article helpful?
If so, consider buying me a coffee over at