I have a string like aaa,aaa,aaa,bbb,bbb,ccc,bbb,cccI want to remove duplicate word from string then output will be like aaa,bbb,cccI tried This code Source$ echo zebra ant spider spider ant zebra ant | xargs -n1 | sort -u | xargsIt is working fine with same value,but when I give my variable value then it is showing all duplicate word also.How can I  remove duplicate value.UPDATEMy question is adding all corresponding value into a single string  if user is same .I have data like this ->   user name    | colour    AAA         | red    AAA         | black    BBB         | red    BBB         | blue    AAA         | blue    AAA         | red    CCC         | red    CCC         | red    AAA         | green    AAA         | red    AAA         | black    BBB         | red    BBB         | blue    AAA         | blue    AAA         | red    CCC         | red    CCC         | red    AAA         | greenIn coding  I fetch all distinct user then I concatenate color string successfully .For that I am using code  -while read the records     if [ $c ==  ]; then  #$c I defined global        c=$colour1    else        c=$c,$colour1     fiWhen I print this $c variable i get the output (For User AAA) red,black,blue,red,green,red,black,blue,red,green,I want to remove duplicate color .Then desired output should be  likered,black,blue,greenFor this desired output i used above code  echo zebra ant spider spider ant zebra ant | xargs -n1 | sort -u | xargsbut it is displaying the output with duplicate values .Likered,black,blue,red,green,red,black,blue,red,green,Thanks
Remove all duplicate word from string using shell script
shell script;shell;text processing;xargs;duplicate
One more awk, just for fun:$ a=aaa bbb aaa bbb ccc aaa ddd bbb ccc$ echo $a | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf(%s%s,$i,FS)}{printf(\n)}'aaa bbb ccc ddd By the way, even your solution works fine with variables:$ b=zebra ant spider spider ant zebra ant $ echo $b | xargs -n1 | sort -u | xargsant spider zebra