3

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?

2 Answers 2

10

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).

Sign up to request clarification or add additional context in comments.

Comments

4

Assuming your variable is var, try this:

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

This should remove the non-ascii characters

1 Comment

I am selecting the other answer since that doesn't involve an external process. Upvoted though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.