Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove non-ascii characters from a variable in shell script

I am grep'ing the output of a command inside shell script and store the result in a variable.

There is a very corner case where this variable might have non-ascii characters because of parse logic used by grep.

Question: How do I remove these non-ascii characters from this variable inside the shell script, so that I can use the variable in the subsequent commands?

like image 554
Sudar Avatar asked Jun 21 '26 02:06

Sudar


2 Answers

If you're using bash, and if your variable is called var, then

"${var//[^[:ascii:]]/}"

will expand to var with all non-ascii characters removed. So:

var_non_ascii=${var//[^[:ascii:]]/}

should do. (This is definitely the best method: no sub-shells and no forks to external processes to bash).

like image 97
gniourf_gniourf Avatar answered Jun 22 '26 16:06

gniourf_gniourf


Assuming your variable is var, try this:

var=$(echo $var | sed 's/[^\x00-\x7F]//g')

This should remove the non-ascii characters

like image 35
Guru Avatar answered Jun 22 '26 17:06

Guru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!