0

is there anyone has an idea about how to remove hidden characters from a string in shell. this is an exemple :

#echo $a;
[root@localhost ~]#

but when i showw hidden characters :

#echo $a | cat -v
[root@localhost ~]# ls ^H^[[K^H^[[K^H^[[Kpwd^H^[[K^H^[[K^H^[[Kls^H^[[K^H^[[Kpwd^H^[[K^H^[[K^H^[[K

i want to delete hidden characters to have this output

#echo $a | cat -v
[root@localhost ~]#
0

Use sed to strip out the non-printing characters:

echo $a | sed 's/[^ -~]//g' | cat -v

Or to store it:

a=$(echo $a | sed 's/[^ -~]//g')
  • hello , thank for your answer but in the output it's give me "[@ ~]# [[[[[[[[ [[[[[[[[[[[" i want to have the output without the hidden characters . – Kamal Ezzaki May 20 at 12:30
  • You have not tried out your answer. Try it against the output of printf '[root@localhost ~]# ls \b\e[K\b\e[[K\b\e[Kpwd\b\e[K\b\e[K\b\e[K' to see how wrong it goes. – JdeBP May 20 at 12:31
  • Ah, you also want to remove escape sequences in addition to non-printing characters. A second sed will accomplish that. I'll edit my answer... – L. Scott Johnson May 20 at 12:33
  • ... or nm, see the post JdeBP linked in the comment to the main question. – L. Scott Johnson May 20 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.