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?
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).
Assuming your variable is var, try this:
var=$(echo $var | sed 's/[^\x00-\x7F]//g')
This should remove the non-ascii characters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With